Javascript 主干收集.重置(数据)未捕获类型错误:未定义不是函数
因此,在我的应用程序中,我通过jquery$.get方法检索一组数据,并尝试使用该数据重置集合,如下所示Javascript 主干收集.重置(数据)未捕获类型错误:未定义不是函数,javascript,backbone.js,backbone.js-collections,Javascript,Backbone.js,Backbone.js Collections,因此,在我的应用程序中,我通过jquery$.get方法检索一组数据,并尝试使用该数据重置集合,如下所示 var self = this; $.get('/api/project/active', function(data) { self.dropdownListProjectCollection.reset(data); }); initialize: function() { $(window).on('resize', this.responsiveMenu);
var self = this;
$.get('/api/project/active', function(data) {
self.dropdownListProjectCollection.reset(data);
});
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;
},
dropdownListProjectCollection
在如下所示的初始化函数中设置
var self = this;
$.get('/api/project/active', function(data) {
self.dropdownListProjectCollection.reset(data);
});
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
返回一个空集合
我将得到什么未捕获类型错误:未定义不是函数
?该错误由
self.dropdownListProjectCollection.reset(数据)
行
发生什么事了?它在我的应用程序的其余部分工作得非常好
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
以及回调中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);
到
假设您的扩展类似于下划线的
extend
方法您确定当前视图是self
吗?您的意思是什么?我在该视图中创建了var self您在initialize
方法?中创建了var self
,我只是问,因为如果你在做ajax,进入另一个没有绑定到当前视图的方法,那么这可能是个问题…@muistooshort我也使用了var that=this,但仍然会得到相同的错误。如果你在JSFIDLE中发布整个程序,我们可以仔细看看。