如何设置工厂属性以使它们彼此独立?

时间:2017-10-09 08:15:07

标签: angularjs

考虑下面的Angularjs'服务'。我想在这个服务中保留所有'条目'相关变量,这样我就可以在控制器中使用它们 - 因为我相信理想的角度模式需要。但是,如果我操纵控制器中的任何变量 - 条目,entries_Sorted,entries_Loaded在服务对象中 - 它们似乎都采用相同的新值。我理解工厂对象是单例,但这些变量不应该是独立的吗?我不期望或理解我所看到的行为。这有用吗?我一定是做错了。

要明确:

如果我使用此服务的返回方法在我的控制器中设置局部变量,然后更新这些局部变量,则服务中的所有三个条目变量将采用新值。

服务代码:

  angular.
  module('core.entry').
  factory('Entry', ['$http', 'Topten', 'Stack', 'User',
    function($http, Topten, Stack, User) {


      var entries = [];
      var entries_Sorted = [];
      var entries_Loaded = [];

      var service = {};

   service.getEntries = function(stackId, callback) { 
        return $http.get('stacks/' + stackId + '/entries/')
            .success(function(data) {
                entries = data["entries"];
                Topten.setToptens(data["topTen"]);
                Stack.setOpenStack(data["stack"]);


            callback(null, data);
            })
            .error(function(err) {

                callback(err, null);
            });
      };
      service.returnEntries = function() {
    return entries;
  }

  service.sortEntries = function(callback) {

    // 1. Loop through entries inner looping on toptens - adding topten score to total score
    for (var i = 0; i < entries.length; i++) {
      var thisEntry = entries[i];

      var totalScore = 0;

      var toptens = Topten.returnToptens();

      for (var j = 0; j < toptens.length; j++) {
        var thisTopten = toptens[j];
        if (thisTopten["entryId"]) {
            if (thisEntry["_id"] == thisTopten["entryId"]._id) {
              totalScore = totalScore + thisTopten["score"];
            }
          }
      }

      thisEntry.totalScore = totalScore;

        // 2. Add net (likes - dislikes) to entry.totalScore
        for (var j = 0; j < thisEntry.votes.length; j++) {
          var thisVote = thisEntry.votes[j]["vote"];

          if (thisVote == "up") {
            thisEntry["up"] = thisEntry["up"] + 1;
          } else if (thisVote == "down") {
            thisEntry["down"] = thisEntry["down"] + 1;
          }
        }

        var netLikes = thisEntry["up"] - thisEntry["down"]; // one point each
        thisEntry["totalScore"] = thisEntry["totalScore"] + netLikes;
      }

      // 3. Sort entries by entry.totalScore and return 
      entries_Sorted = entries.sort(function(a, b) {
        return b.totalScore - a.totalScore;
      });

      callback();
  };

  service.returnEntries_Sorted = function() {
    return entries_Sorted;
  };

  return service;
}

]);

我的控制器代码:

Entry.getEntries($routeParams.stackId, function(err, data) {
          if(err) {

          }


          // get sorted entries (after return from getEntries)
          Entry.sortEntries(function() {
            self.entries_Sorted = Entry.returnEntries_Sorted();
            self.loadMore();
          });

        });

 self.loadMore = function() {

      self.entries_Loaded = self.entries_Loaded.concat(self.entries_Sorted.splice(page * increment, increment));
      self.page +=1;
    }

问题:在我调用这个本地'load_More'函数后,我的服务中的属性 - entries,_ Sorted,_Loaded - 都将具有新的'拼接'值。即。 Entry.entries将具有与控制器的本地self.entries_Sorted相同的值。

0 个答案:

没有答案