私は CartItem という名前のモデルを持っています:
var CartItem = Backbone.Model.extend({
// Will contain three attributes.
// These are their default values
defaults: {
plucode: 0,
title: 'cnpdx.cart',
discount: 100,
qty: 5,
price: 100,
extendcode: 0,
checked: false,
salemode: 1,
comcode: 0,
publisher: '',
guide: '3',
guidename: '现货'
},
toggle: function () {
this.set('checked', !this.get('checked'));
}
});
そして、CartList という名前のコレクション モデル:
var CartList = Backbone.Collection.extend({
model: CartItem,
defaults: {
totalFixPrice: 0,
totalQty: 0,
totalDiscountPrice: 0,
totals: 0,
checked: false
},
getChecked: function () {
return this.where({checked: true});
},
getByComcode: function (comcode) {
return this.where({comcode: comcode});
}
toggle: function () {
this.set('checked', !this.get('checked'));
this.model.set('checked', !this.get('checked'));
}
});
そして、いくつかのカートアイテムでコレクションを作成します:
var cartList = new CartList([
new CartItem({plucode:'123451', title: 'web development',discount:80,qty:5, price: 200,extendcode:'123451',salemode:1,comcode:'7-301',publisher:'北京大学出版社',guide:3,guidename:'现货'}),
new CartItem({plucode:'123452', title: 'web design', discount:80,qty:5,price: 250,extendcode:'123452',salemode:1,comcode:'7-301',publisher:'北京大学出版社',guide:3,guidename:'现货'}),
new CartItem({plucode:'123453', title: 'photography', discount:80,qty:5,price: 100,extendcode:'123451,123452',salemode:2,comcode:'7-301',publisher:'北京大学出版社',guide:3,guidename:'现货'}),
new CartItem({plucode:'123454', title: 'coffee drinking', discount:80,qty:5,price: 10,extendcode:'123451,123452',salemode:2,comcode:'7-301',publisher:'北京大学出版社',guide:3,guidename:'现货'}),
new CartItem({plucode: '123421', title: 'web development', discount: 80, qty: 5, price: 200, extendcode: '123421', salemode: 1,comcode:'7-302',publisher:'清华大学出版社',guide:3,guidename:'现货'}),
new CartItem({plucode: '123422', title: 'web design', discount: 80, qty: 5, price: 250, extendcode: '123422', salemode: 1,comcode:'7-302',publisher:'清华大学出版社',guide:3,guidename:'现货'}),
new CartItem({plucode: '123423', title: 'photography', discount: 80, qty: 5, price: 100, extendcode: '123421,123422', salemode: 2,comcode:'7-302',publisher:'清华大学出版社',guide:3,guidename:'现货'}),
new CartItem({plucode: '123424', title: 'coffee drinking', discount: 80, qty: 5, price: 10, extendcode: '123421,123422', salemode: 2,comcode:'7-302',publisher:'清华大学出版社',guide:3,guidename:'现货'})
// Add more here
]);
今、私は「comcode」でcartListをグループ化したいので、
_.groupBy(cartList, 'comcode')
しかし、それはエラーが発生します:
Uncaught TypeError: Cannot read property 'comcode' of undefined
私たちを手伝ってくれますか?