Javascript Backbone.js this.collection.models在渲染视图时工作,但在其他地方调用时返回空数组
伙计们,很抱歉这么长的代码,但我有一个简单的问题。为什么我的代码在使用该方法时第一次调用时呈现this.collection时包含元素,并且由于我的代码结构,它呈现我的数据库对象并完美地工作。但是,当我在getTypes函数上再次调用this.collection时,我得到一个空数组,我不知道发生了什么事???调用的是什么Javascript Backbone.js this.collection.models在渲染视图时工作,但在其他地方调用时返回空数组,javascript,backbone.js,Javascript,Backbone.js,伙计们,很抱歉这么长的代码,但我有一个简单的问题。为什么我的代码在使用该方法时第一次调用时呈现this.collection时包含元素,并且由于我的代码结构,它呈现我的数据库对象并完美地工作。但是,当我在getTypes函数上再次调用this.collection时,我得到一个空数组,我不知道发生了什么事???调用的是什么getTypes,何时调用?注意:不要使用每个(this.collection.models,…,使用这个.collection.each(…)相反。当然,伙计,我会继续修改u
getTypes
,何时调用?注意:不要使用每个(this.collection.models,…
,使用这个.collection.each(…)
相反。当然,伙计,我会继续修改u3;。每次我都觉得最好使用下划线属性。我意识到get类型是在初始化之前调用的,所以我猜它是在渲染之前调用的。我必须查看我的代码库,试着找出为什么…主干混合了一些在这两个版本中都加下划线,因此通常最好使用混合版本。如果一个集合在您不希望它是空的情况下是空的,那么您通常会在它的获取
底层的AJAX调用完成之前尝试访问它;解决方案通常是(a)对空集合进行明智的操作,以及(b)响应适当的收集事件以执行“收集不是空的”操作。
var Order = Backbone.Model.extend({
url: function() {
return 'http://localhost:51782/api/orders/'+this.id;
}
});
var DataSetOrders = Backbone.Collection.extend({
url: "http://localhost:51782/api/orders",
model: Order,
initialize: function(){
this.fetch({
success: this.fetchSuccess,
error: this.fetchError
});
},
// This is where my data is being extracted I am returning the
// response since its an array of all the objects within my database
// perhaps i should not use return? Is there a way to save this information so it can always
// be available when i call DataSetOrders();?
fetchSuccess: function (collection, response) {
// console.log('Collection fetch success', response);
// console.log('Collection models: ', collection.models);
return response;
},
fetchError: function (collection, response) {
throw new Error("Orders fetch error");
}
});
var DataSetOrdersView = Backbone.View.extend({
el: $("#orders"),
// collection: new DataSetOrders(),
initialize: function () {
this.collection = new DataSetOrders();
this._modelBinder = new Backbone.ModelBinder();
this.render();
this.$el.find("#filter").append(this.createSelect());
this.on("change:filterType", this.filterByStatus, this);
this.collection.on("reset", this.render, this);
this.collection.on("add", this.renderOrder, this);
this.collection.on("remove", this.removeOrder, this);
},
render: function () {
this.$el.find("article").remove();
// This is the line where we are getting all of our models and rendering it to the #orders view
// as you can see it is referencing the this.collection = new DataSetOrders()
// This is actually working the view is being rendered and I can see the orders being pulled from
// the database
_.each(this.collection.models, function (item) {
this.renderOrder(item);
}, this);
},
renderOrder: function (item) {
// The item which is an element of the previous array returned is now being placed in the model
// and its being rendered prefectly.
var orderView = new OrderView({
model: item
});
this.$el.append(orderView.render().el);
// this.$el.append(this._modelBinder.bind(orderView.render().el));
},
getTypes: function () {
// HERE!!! IS where this.collection is now = [] wtf?!? I dont know why its doing this pleace help me!!
return _.uniq(this.collection.pluck("OrderStatus"), false, function (type) {
return type.toLowerCase();
});
},