如何使用我自己的索引查询特定用户信息并将其传递给 $firebaseArray 以便与我的视图同步?
How can I query specific user info with my own indexes and pass it to a $firebaseArray in order to sync with my view?
我的 firebase 数据库中有以下结构:
{
rooms: {
LKVmdIPX2_235ksf: {
members: {
poasj12O-s657-235a-1236-9832lksf976c: true
},
name: Room 1,
public: true
},
KS0sk21gleascm14: {
members: {
poasj12O-s657-235a-1236-9832lksf976c: true
},
name: Room 2,
public: true
}
},
users: {
poasj12O-s657-235a-1236-9832lksf976c: {
rooms: {
LKVmdIPX2_235ksf: true,
KS0sk21gleascm14: true
},
name: Filler
email: filler@filler.com
}
}
为了从特定用户(在本例中为用户 = poasj12O-s657-235a-1236-9832lksf976c)获取每个房间,我做了以下操作:
$scope.rooms = {};
var ref = new Firebase('URL');
var userRef = ref.child('users').child(uid); // I'm manage to get the user's UID after login;
var roomRef = ref.child('rooms');
userRef.child('rooms').on('child_added', function(data) {
roomRef.child(data.key()).on('value', function(rData) {
console.log(rData.val()); // prints each room object
$scope.rooms = $firebaseArray(rData.ref());
});
});
所以为了能够在我尝试过的视图中显示该信息:
$scope.rooms = $firebaseArray(rData.ref());
问题是当我这样做时 console.log($scope.rooms)
我只是得到一个空对象,而且如果我把它放在视图中 {{rooms}}
它甚至会显示用户拥有的所有房间。
那么我的问题是,如何使用我自己的索引查询特定用户信息并将其传递给 $firebaseArray 以便与我的视图同步?
OBS:console.log(rData.val())
打印我希望在 $scope.room 变量中拥有的正确对象。
这是我找到 "query" https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html
的参考
你必须注意到 child_added 和 value 事件是 async。如果你把 console.log 放在控制器的底部,你可能会看到一个空的 object/array 因为它们在执行时还没有填充..
不要忘记在您的异步请求中调用 $scope.$apply 以让您的视图了解变量中的新值。
(function() {
app.controller('lobbyController',
function($rootScope, $scope, $state, $ionicHistory, $firebaseArray, $firebaseObject, firebaseObj, nodes) {
$scope.rooms = []; // initialize an empty array
$scope.campaigns = {};
var uid = $rootScope.authData.uid;
var userInfo;
userRef = firebaseObj.child(nodes.USER_NODE).child(uid);
campaignRef = firebaseObj.child(nodes.CAMPAIGN_NODE);
userRef.child(nodes.CAMPAIGN_NODE).on('child_added', function(data) {
campaignRef.child(data.key()).on('value', function(cData) {
// force the view to rerender after a new value came from firebase
$scope.$apply(function() {
// push the room to rooms array
$scope.rooms.push(cData.val());
})
});
});
$scope.createCampaign = function() {
$state.go('createCampaign');
}
}
);
})();
调试 $scope 的一个好习惯是将它暴露给 window 对象,然后在浏览器控制台上检查它。在您的控制器中放入以下代码:
window.$scope = $scope;
我的 firebase 数据库中有以下结构:
{
rooms: {
LKVmdIPX2_235ksf: {
members: {
poasj12O-s657-235a-1236-9832lksf976c: true
},
name: Room 1,
public: true
},
KS0sk21gleascm14: {
members: {
poasj12O-s657-235a-1236-9832lksf976c: true
},
name: Room 2,
public: true
}
},
users: {
poasj12O-s657-235a-1236-9832lksf976c: {
rooms: {
LKVmdIPX2_235ksf: true,
KS0sk21gleascm14: true
},
name: Filler
email: filler@filler.com
}
}
为了从特定用户(在本例中为用户 = poasj12O-s657-235a-1236-9832lksf976c)获取每个房间,我做了以下操作:
$scope.rooms = {};
var ref = new Firebase('URL');
var userRef = ref.child('users').child(uid); // I'm manage to get the user's UID after login;
var roomRef = ref.child('rooms');
userRef.child('rooms').on('child_added', function(data) {
roomRef.child(data.key()).on('value', function(rData) {
console.log(rData.val()); // prints each room object
$scope.rooms = $firebaseArray(rData.ref());
});
});
所以为了能够在我尝试过的视图中显示该信息:
$scope.rooms = $firebaseArray(rData.ref());
问题是当我这样做时 console.log($scope.rooms)
我只是得到一个空对象,而且如果我把它放在视图中 {{rooms}}
它甚至会显示用户拥有的所有房间。
那么我的问题是,如何使用我自己的索引查询特定用户信息并将其传递给 $firebaseArray 以便与我的视图同步?
OBS:console.log(rData.val())
打印我希望在 $scope.room 变量中拥有的正确对象。
这是我找到 "query" https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html
的参考你必须注意到 child_added 和 value 事件是 async。如果你把 console.log 放在控制器的底部,你可能会看到一个空的 object/array 因为它们在执行时还没有填充..
不要忘记在您的异步请求中调用 $scope.$apply 以让您的视图了解变量中的新值。
(function() {
app.controller('lobbyController',
function($rootScope, $scope, $state, $ionicHistory, $firebaseArray, $firebaseObject, firebaseObj, nodes) {
$scope.rooms = []; // initialize an empty array
$scope.campaigns = {};
var uid = $rootScope.authData.uid;
var userInfo;
userRef = firebaseObj.child(nodes.USER_NODE).child(uid);
campaignRef = firebaseObj.child(nodes.CAMPAIGN_NODE);
userRef.child(nodes.CAMPAIGN_NODE).on('child_added', function(data) {
campaignRef.child(data.key()).on('value', function(cData) {
// force the view to rerender after a new value came from firebase
$scope.$apply(function() {
// push the room to rooms array
$scope.rooms.push(cData.val());
})
});
});
$scope.createCampaign = function() {
$state.go('createCampaign');
}
}
);
})();
调试 $scope 的一个好习惯是将它暴露给 window 对象,然后在浏览器控制台上检查它。在您的控制器中放入以下代码:
window.$scope = $scope;