Backbone collection.reset(data)Uncaught TypeError:undefined不是函数

时间:2013-11-05 16:02:49

标签: javascript backbone.js backbone-collections backbone.js-collections

所以在我的应用程序中,我通过jquery $ .get方法检索一组数据,并试图用这样的数据重置一个集合,

var self = this;

$.get('/api/project/active', function(data) {
    self.dropdownListProjectCollection.reset(data);
});

dropdownListProjectCollection在初始化函数中设置,如下所示,

initialize: function() {
    $(window).on('resize', this.responsiveMenu);

    this.dropdownListProjectCollection = new app.ProjectCollection;

    console.log(this.dropdownListProjectCollection);

    this.dropdownListProjectCollection.on('reset', this.populateMenu, this);

    this.render();

    return this;
},

console.log返回一个空集合。

我会得到什么Uncaught TypeError: undefined is not a function

触发错误

self.dropdownListProjectCollection.reset(data)行。

怎么回事?它在我的应用程序的其余部分完美运行。

getActiveProjects方法中的console.log自我

r {cid: "view1", options: Object, $el: e.fn.e.init[1], el: div.contentwrap, dropdownListProjectCollection: ProjectCollection…}  
$el: e.fn.e.init[1]
cid: "view1"
dropdownListProjectCollection: ProjectCollection
el: div.contentwrap
options: Object
__proto__: s

和回调中的自我控制台。

r {cid: "view1", options: Object, $el: e.fn.e.init[1], el: div.contentwrap, dropdownListProjectCollection: ProjectCollection…}
$el: e.fn.e.init[1]
cid: "view1"
dropdownListProjectCollection: ProjectCollection
el: div.contentwrap
options: Object
__proto__: s

重置前的dropdownListProjectCollection

    ProjectCollection {length: 0, models: Array[0], _byId: Object, _events: Object, constructor: function…}
_byId: Object
_events: Object
length: 0
models: Array[0]
__proto__: ctor

PRoject Collection

    ProjectCollection = (function(_super) {

    __extends(ProjectCollection, _super);

    function ProjectCollection() {
      return ProjectCollection.__super__.constructor.apply(this, arguments);
    }

    ProjectCollection.prototype.url = "/api/project/projects";

    ProjectCollection.prototype.model = app.Project;

    ProjectCollection.prototype.archived = function() {
      return new ProjectCollection(this.where({
        status: '3'
      }));
    };

    ProjectCollection.prototype.active = function() {
      return new ProjectCollection(this.where({
        status: '1'
      }));
    };

    ProjectCollection.prototype.pending = function() {
      return new ProjectCollection(this.where({
        status: '7'
      }))
    };

    ProjectCollection.prototype.completed = function() {
      return new ProjectCollection(this.where({
        status: '5'
      }));
    }

    ProjectCollection.prototype.comparator = function(model) {
      return -model.get("creation_date_unix");
    };

    ProjectCollection.prototype.search = function(searchTerm, filters) {
      var pattern, status = [];
      pattern = new RegExp(searchTerm, "gi");

      // Loop throught the filters and push there numeric value to an array.
      for (var k in filters) {
        if(k == "pending" && filters["pending"] == true) {
          var pending = this.pending();
        }
        if(k == "active" && filters["active"] == true) {
          var active = this.active();
        }
        if(k == "completed" && filters["completed"] == true) {
          var completed = this.completed();
        }
        if(k == "archived" && filters["archived"] == true) {
          var archived = this.archived();
        }
      }


      var filteredCollection = new ProjectCollection;

      if(pending !== undefined) {
        filteredCollection.add(pending.models);
      }
      if(active !== undefined) {
        filteredCollection.add(active.models);
      }
      if(completed !== undefined) {
        filteredCollection.add(completed.models);
      }
      if(archived !== undefined) {
        filteredCollection.add(archived.models);
      }

      if(searchTerm != "") {
        return _(filteredCollection.filter(function(project){
          return pattern.test(project.get("project_name") + project.get("client_name"));
        }));
      }

      return filteredCollection;






      /*// Filter the collection based on the status attribute of 
      // the model. If the value of a key is undefined (not met the criteria in the loop)
      // the value will be undefined and return empty (false).
      var filteredCollection = this.filter(function(project){
        return project.get("status") == status[0] ||
               project.get("status") == status[1] ||
               project.get("status") == status[2] ||
               project.get("status") == status[3] ;
      });

      console.log(filteredCollection);
      */



    };

    return ProjectCollection;

  })(app.BaseCollection);

1 个答案:

答案 0 :(得分:0)

你可以改变这个

吗?
__extends(ProjectCollection, _super);

__extends(ProjectCollection.prototype, _super);

假设__extends类似于下划线的extend方法