我有一个通知模型
// MODEL
NotificationModel = App.BB.Model.extend({
defaults : {}
});
// COLLECTION
NotificationCollection = App.BB.Collection.extend({
model: NotificationModel,
url: '/notifications',
initialize : function() {
var me = this;
me.fetch();
}
});
该集合正确地从服务器获取,并具有以下字段(id、read),其中 read 为 true 或 false。
如何获取已读取的项目总数 == false? ...未读项目数?
谢谢
最佳答案
结构化解决方案是在您的集合中创建这些方法:
read: function() {
return this.filter(function(n) { return n.get('read'); });
},
unread: function() {
return this.filter(function(n) { return !(n.get('read')); });
}
如果您需要计数,只需将 .length
添加到方法末尾即可。
关于jquery - 使用 Backbone.js 和 Underscore,如何从模型中获取项目数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529791/