2

我有一个名为 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'));
    }

});

我用一些caritems创建了一个集合:

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”对购物车列表进行分组,所以我使用

_.groupBy(cartList, 'comcode')

但它发生错误:

Uncaught TypeError: Cannot read property 'comcode' of undefined 

你可以帮帮我吗?

4

1 回答 1

6

您正在尝试按骨干集合进行分组,但是您使用的方式_.groupBy()假定您有一个 JSON 对象数组,而实际上不是骨干集合。该comcode属性是集合的模型属性的一个属性。骨干集合和模型不会将它们的属性存储为集合/模型本身的直接属性。它们存储在 attributes 属性中,并使用getset函数访问。因此CartItem.comcode不存在且未定义。要访问comcode模型的属性,您需要执行以下操作:

var cartitem =new CartItem({plucode:'123451', title: 'web development',discount:80,qty:5, price: 200,extendcode:'123451',salemode:1,comcode:'7-301',publisher:'北京大学出版社',guide:3,guidename:'现货'})

var comcode = cartitem.get("comcode ");

因此,根据您的情况进行分组,您可以执行以下任一操作:

_.groupBy(cartList.models, function (cartitem) {
    cartitem.get("comcode ");
})

甚至更好,因为 Backbone 代理了 Underscore 的 groupBy 函数:

cartList.groupBy(function (cartitem) {
    cartitem.get("comcode ");
})
于 2013-06-07T01:50:38.917 回答