Backbone 对象属性上的函数
Backbone Where function on an attribute which is an object
我有一个像这样的 backbone 模型:
Name: Test1
size: {Key: "M" Value: "Med"}
现在我正在尝试获取密钥 M 的总数。下面的函数不起作用:
getTotals: function(property) {
return this.where({ size: { Key: property }}).length;
}
我这样称呼它:
collection.getTotals("M")
有没有办法用 backbone where 函数做到这一点?
我认为 backbone 的 where
方法无法做到这一点。查看文档 (http://backbonejs.org/#Collection-where) 并考虑它所说的内容:"useful for simple cases of filter"(强调已添加)。
你的不是简单的情况,因为size
的值是一个对象,但是我们还是用filter:
collection.filter(function(model) {
return model.get('size').Key == 'M'
});
我有一个像这样的 backbone 模型:
Name: Test1
size: {Key: "M" Value: "Med"}
现在我正在尝试获取密钥 M 的总数。下面的函数不起作用:
getTotals: function(property) {
return this.where({ size: { Key: property }}).length;
}
我这样称呼它:
collection.getTotals("M")
有没有办法用 backbone where 函数做到这一点?
我认为 backbone 的 where
方法无法做到这一点。查看文档 (http://backbonejs.org/#Collection-where) 并考虑它所说的内容:"useful for simple cases of filter"(强调已添加)。
你的不是简单的情况,因为size
的值是一个对象,但是我们还是用filter:
collection.filter(function(model) {
return model.get('size').Key == 'M'
});