我的这段代码一直在抛出错误,我不明白为什么。
var id = $(ev.currentTarget).data("id");
var item = ItemCollection.getByCid(id);
alert(item.get("ItemCode"));
var qty = 1;
var cartcollection = new CartCollection();
cartcollection.add( item );
CartListView.render();
var itemcode = cartcollection.where({ItemCode: item.get("ItemCode")});
if( itemcode.length > 0 ){ alert("success"); }
所以我想做的是检查CartCollection是否已经具有相同的模型,如果是,则它应该只更新模型的Qty atrib。现在基于该代码返回CartCollection不是函数或不是构造函数。为什么会这样!?有任何想法吗?感谢
我正在使用主干,需要,KendoUI网格和下划线,所以我的代码就是这样:
itemlist_view.js
define([
'jquery',
'underscore',
'backbone',
'model/item_model',
'model/cart_model',
'collection/item_collection',
'collection/cart_collection',
'view/cart/cartlist_view',
'text!templates/items/itemlist.html'
],function($, _, Backbone, Item, Cart, ItemCollection, CartCollection, CartListView, ItemListTemplate){
var ItemListView = Backbone.View.extend({
el: $("#mainContainer"),
events:{
"click #itemListContainer li" : "AddToCart"
},
initialize: function(){
this.model = Item;
this.collection = ItemCollection;
this.collection.bind("reset", this.render );
},
render: function(){
var data = {
items: ItemCollection.models
}
var compiledTemplate = _.template( ItemListTemplate , data);
$("#itemContainer").html( compiledTemplate );
},
AddToCart:function(ev){
ev.preventDefault();
var id = $(ev.currentTarget).data("id");
var item = ItemCollection.getByCid(id);
alert(item.get("ItemCode"));
var qty = 1;
var cartcollection = new CartCollection();
cartcollection.add( item );
CartListView.render();
var itemcode = cartcollection.where({ItemCode: item.get("ItemCode")});
if( itemcode.length > 0 ){ alert("success"); }
}
});
return new ItemListView;
});
cart_collection.js
define([
'underscore',
'backbone',
'model/cart_model'
],function(_, Backbone, Cart){
var CartCollection = Backbone.Collection.extend({
model: Cart,
initialize: function(){
}
});
return new CartCollection;
});
cartlist_view.js
define([
'jquery',
'underscore',
'backbone',
'model/cart_model',
'collection/cart_collection',
'text!templates/cart/cartlist.html'
], function($, _, Backbone, Cart, CartCollection, CartListTemplate){
var Model = kendo.data.Model,
ObservableArray = kendo.data.ObservableArray;
function wrapBackboneModel(backboneModel, fields) {
return Model.define({
fields: fields,
init: function(model) {
if (!(model instanceof backboneModel)) {
model = new backboneModel(model);
}
Model.fn.init.call(this, model.toJSON());
this.backbone = model;
},
set: function(field, value) {
Model.fn.set.call(this, field, value);
this.backbone.set(field, value);
}
});
}
function wrapBackboneCollection(model) {
return ObservableArray.extend( {
init: function(collection) {
ObservableArray.fn.init.call(this, collection.models, model);
this.collection = collection;
},
splice: function(index, howMany) {
var itemsToInsert, removedItemx, idx, length;
itemsToInsert = Array.prototype.slice.call(arguments, 2);
removedItems = kendo.data.ObservableArray.fn.splice.apply(this, arguments);
if (removedItems.length) {
for (idx = 0, length = removedItems.length; idx < length; idx++) {
this.collection.remove(removedItems[idx].backbone);
}
}
if (itemsToInsert.length) {
for (idx = 0, length = itemsToInsert.length; idx < length; idx++) {
this.collection.unshift(itemsToInsert[idx].backbone);
}
}
return removedItems;
}
});
}
kendobackboneCollection = wrapBackboneCollection;
kendobackboneModel = wrapBackboneModel;
var CartListView = Backbone.View.extend({
el: $("#cartContainer"),
initialize: function(){
this.collection = CartCollection;
this.model = Cart;
this.collection.bind("change", this.render );
},
render: function(){
console.log("here");
this.el.html(CartListTemplate);
var CartWrapper = kendobackboneModel(Cart, {
ItemCode: { type: "string" },
ItemDescription: { type: "string" },
RetailPrice: { type: "string" },
Qty: { type: "string" },
});
var CartCollectionWrapper = kendobackboneCollection(CartWrapper);
this.$("#grid").kendoGrid({
editable: true,
toolbar: ["create"],
columns: ["ItemDescription", "Qty", "RetailPrice"],
dataSource: {
schema: {model: CartWrapper},
data: new CartCollectionWrapper(CartCollection),
}
});
},
});
return new CartListView;
});
答案 0 :(得分:0)
我认为问题在于你是双重实例CartCollection
。也就是说,cart_collection.js
会返回new CartCollection()
,而itemlist_view.js
会再次使用var cartcollection = new CartCollection();
对其进行实例化。
将该行更改为var cartcollection = CartCollection;
并查看其行为。显然,您也可以删除cartcollection
变量并将其用法替换为CartCollection
。
另一方面,我会认真考虑不从你的new CartCollection()
返回cart_collection.js
。这感觉非常糟糕,因为您只能使用该集合的一个实例。此外,开发人员并不明白他们正在收回集合的实例,而不是它的构造函数。
我建议您返回CartCollection
instaed,然后使用itemlist_view.js
在new CartCollection()
文件中对其进行实例化。这样,您可以在以后需要时实例化更多这些集合。
同样建议cartlist_view.js
返回new CartListView()
。