【问题标题】:Can I use $$updated when extending a firebase factory to run a getTotal() function like in the example?在扩展 Firebase 工厂以运行示例中的 getTotal() 函数时,我可以使用 $$updated 吗?
【发布时间】:2015-02-06 09:52:51
【问题描述】:

我是否可以接收 $$updated 返回的内容,或者让 $$updated 运行一个函数,然后每次检查任务时我都可以从该函数接收?

归根结底,我需要统计用户完成了多少任务。似乎firebase有自动同步这些数据的方法,但目前还不清楚具体如何做到这一点。当任务完成时,我遇到了 $watch 和运行函数的问题。这看起来是一种有趣的方式,但我无法将这些部分组合在一起。

这里是下面代码的工作 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
  };
}]);

【问题讨论】:

    标签: javascript angularjs firebase angularfire


    【解决方案1】:

    如果您想要所有已完成任务的最新统计,您可以在保存任务的位置添加then

    $scope.listWithTotal.$save(task).then(function() {
      $scope.tallyCount = $scope.listWithTotal.getTotal();
    });
    

    then 块在任务保存后执行,并将已完成任务的计数添加到当前范围。

    【讨论】:

    • 感谢您的回复。这就是我现在的做法。应该有一种方法可以将此功能直接绑定到阵列,以便每当添加/删除/更改任务时,更新都会在服务器/本地自动发生。这就是我想要弄清楚的。
    • 不确定这是否是您要查找的内容,但视图中的此绑定有效:completed count: {{listWithTotal.getTotal()}}
    • 这会起作用(可能,在我的情况下,我有一系列用户,但这是不相关的)但我想看看我是否可以通过覆盖 $$updated 函数来做到这一点。似乎在 $firebase 数组中具有该功能会更清洁且更易于维护。我将需要做其他事情,例如创建任务副本并将它们移动到不同的列表等。
    • {{listWithTotal.getTotal()}} 中对 FirebaseArray 扩展中的逻辑 is 进行采样。但是你从$$updated 得到的回报不会在你的范围内,这似乎是你所期望的。您将要么 必须更新范围(这是我的回答所做的) 绑定到数组的方法(这是我之前的评论所做的)。
    • 是的,我希望有一种方法可以在服务器上进行更新(在运行 $$updated 时运行 getTotal 函数)触发 $scope 上的事件来更新该值。 {{listWithTotal.getTotal}} 会这样做,但这会根据我的理解不断运行该功能。仅在完成任务时运行它意味着当任务移至未完成或删除任务时我需要再次运行它——它会产生更多我需要考虑的边缘情况。
    猜你喜欢
    • 2011-12-21
    • 2016-08-16
    • 2021-04-09
    • 2016-11-13
    • 2020-06-15
    • 1970-01-01
    • 2012-10-24
    • 2018-10-20
    • 2017-05-23
    相关资源
    最近更新 更多