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();
});
},
抱歉长码,但我有一个简单的问题。为什么我的代码在使用方法时第一次调用render.collection具有其中的元素,并且由于我的代码结构,它呈现我的数据库对象并且完美地工作。但是当我在geTypes函数上再次调用this.collection时,我得到一个空数组,我不明白它是怎么回事?