Emp*_*ets 11 javascript angularjs firebase angularfire
我是否可以收到$$更新返回的内容或者已更新$$运行一个函数,我可以在每次检查任务时收到该函数?
在一天结束时,我需要计算用户完成的任务数量.似乎firebase有自动同步数据的方法,但目前还不清楚具体如何做到这一点.当任务完成时,我遇到了$ watch和running函数的问题.这看起来像一个有趣的方式,但我不能把这些碎片放在一起.
以下是代码的plnkr:http://plnkr.co/edit/iAGvPHFWn2GSPGzBRpKh?p = preview
// Code goes here
angular.module('app', ['firebase']);
angular
.module('app')
.controller('main', function($scope, $firebase, $timeout, $window, ListWithTotal) {
var ref = new Firebase("https://plnkr.firebaseio.com");
$scope.listWithTotal = ListWithTotal(ref);
$scope.addTask = function(task) {
$scope.listWithTotal.$add({
title: task.title,
complete: false,
tally: 0
});
task.title = '';
};
$scope.completeTask = function(task) {
if (task.complete === true) {
task.complete = true;
task.tally = 1;
} else {
task.complete = false;
task.tally = 0;
}
$scope.listWithTotal.$save(task);
};
$scope.tallyCount = '';
//it would be cool if I can get tallyCount to receive
//the result of getTotal or automagically with $$updated.
}).factory("ListWithTotal", ["$FirebaseArray", "$firebase", function($FirebaseArray, $firebase) {
// create a new factory based on $FirebaseArray
var TotalFactory = $FirebaseArray.$extendFactory({
getTotal: function() {
var total = 0;
angular.forEach(this.$list, function(rec) {
total += rec.tally;
});
return total;
},
$$updated: function(){
return this.$list.getTotal();
}
});
return function(listRef) {
var sync = $firebase(listRef, {arrayFactory: TotalFactory});
return sync.$asArray(); // this will be an instance of TotalFactory
};
}]);
Run Code Online (Sandbox Code Playgroud)
如果您想要了解所有已完成任务的最新记录,可以添加then到保存任务的位置:
$scope.listWithTotal.$save(task).then(function() {
$scope.tallyCount = $scope.listWithTotal.getTotal();
});
Run Code Online (Sandbox Code Playgroud)
该then任务保存后执行该块,并将已完成任务的计数添加到当前作用域.
| 归档时间: |
|
| 查看次数: |
207 次 |
| 最近记录: |