0

因此,在我的应用程序中,我通过 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);
4

1 回答 1

0

can you change this

__extends(ProjectCollection, _super);

to

__extends(ProjectCollection.prototype, _super);

assuming you __extends is similar to underscore's extend methods

于 2013-11-11T09:08:23.760 回答