我正在尝试编码这种情况。想象一下这个结构:
Users
->john123
-> name : john johnson
-> clients:
->1: true
->2: true
->4: true
->thomasiscool
-> name : thomas thomson
-> clients
->3: true
Clients
->1
->info: ...
->2
->info: ...
->3
->info: ...
->4
->info: ...
我正在阅读Firebase博客,但我似乎无法理解如何展示,例如特定用户的客户端。 例如,john123已登录。他想查看其客户的表(ngrepeat)。它会显示客户端1,2和4。 我该如何编码?
答案 0 :(得分:0)
你走了。它看起来有点复杂,但一旦您了解了Firebase的基础知识,它就会变得更加容易。通过此设置,当您将客户端添加到用户的配置文件中时,它将动态更新客户端列表。您还可以查看实用程序库Firebase.Util,它会将所有这些内容减少到大约3行代码。
工作Fiddle
Firebase网址:Firebase
app.controller('mainCtrl', function($firebase, $scope, Client){
$scope.allClients = Client.all;
$scope.findClients = function(){
$scope.clients = Client.findByRef($scope.name);
}
});
app.factory('Client', function($firebase, $rootScope, $timeout){
var ref = new Firebase($rootScope.demoUrl + '/clients');
var clients = $firebase(ref).$asArray();
var Client = {
all: clients,
findByRef: function(user){
console.log(user);
var indexRef = new Firebase($rootScope.demoUrl+ '/users/' + user + '/clients');
var clientRef = new Firebase($rootScope.demoUrl+ '/clients');
var tempClients = {};
indexRef.on('child_added', function(ss1){
var clientId = ss1.name();
clientRef.child(clientId).on('value', function(ss2){
$timeout(function(){
tempClients[clientId] = ss2.val();
});
})
});
return tempClients;
}
}
return Client;
});