我有一个待办事项列表,可以跟踪已完成的任务数量。我正在尝试扩展工厂以添加sum()函数。这是一个文档链接,它解释了我想要做的事情: https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-extending-the-factories-firebasearrayextendfactory
我可以得到它通常工作,但我必须刷新页面,以使其更新sum()值。如果我做一个常规$ watch,那么它会崩溃浏览器并导致一个痛苦的世界。如何在每次完成任务时更新sum()值?
工作解决方案:
function tallyUp(){
var tallllyProfiles = $firebase(new Firebase('https://demo.firebaseio.com/teams/' + team + '/profiles/')).$asObject();
tallllyProfiles.$loaded().then(function(data) {
angular.forEach(tallllyProfiles, function(value, key, tallllyProfile) {
var user = value.uid;
var ref = new Firebase('https://demo.firebaseio.com/teams/' + team + '/profiles/' + user + '/todo/');
$scope.list = $firebase(ref, {arrayFactory: "TallllyFactory"}).$asArray();
$scope.list.$loaded().then(function(data){
value.tallyCount = data.sum();
tallllyProfile.$save();
});
});
});
}
tallyUp();
$scope.$watch('activity', function(){
console.log('this ran');
tallyUp();
});
谢谢!