簡體   English   中英

骨干collection.reset(data)未捕獲TypeError:undefined不是函數

[英]Backbone collection.reset( data ) Uncaught TypeError: undefined is not a function

因此,在我的應用程序中,我通過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方法內部的self的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

和回調中self的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

重置之前的dropdownListProjectCollection

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

項目集合

    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);

你能改變這個嗎

__extends(ProjectCollection, _super);

__extends(ProjectCollection.prototype, _super);

假設__extends與下划線的extend方法相似

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM