首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用underscore.js的groupBy按照字符串属性对Backbone.js中的集合进行分组?

如何使用underscore.js的groupBy按照字符串属性对Backbone.js中的集合进行分组?
EN

Stack Overflow用户
提问于 2013-06-07 09:39:59
回答 1查看 2.8K关注 0票数 2

我有一个名为CartItem的模型:

代码语言:javascript
运行
复制
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的集合模型:

代码语言:javascript
运行
复制
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'));
    }

});

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

代码语言:javascript
运行
复制
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
]);

现在,我希望通过“cartList”对comcode进行分组,所以我使用

代码语言:javascript
运行
复制
_.groupBy(cartList, 'comcode')

但它会出现一个错误:

代码语言:javascript
运行
复制
Uncaught TypeError: Cannot read property 'comcode' of undefined 

你能帮我一下吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-07 09:50:39

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

代码语言:javascript
运行
复制
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 ");

因此,在您的示例中,要进行分组,您可以执行以下任一操作:

代码语言:javascript
运行
复制
_.groupBy(cartList.models, function (cartitem) {
    cartitem.get("comcode ");
})

或者更好,因为主干代理强调了groupBy的功能:

代码语言:javascript
运行
复制
cartList.groupBy(function (cartitem) {
    cartitem.get("comcode ");
})
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16974974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档