AnA*_*ice 3 jquery backbone.js underscore.js
我有通知的通知模型
// MODEL
NotificationModel = App.BB.Model.extend({
defaults : {}
});
// COLLECTION
NotificationCollection = App.BB.Collection.extend({
model: NotificationModel,
url: '/notifications',
initialize : function() {
var me = this;
me.fetch();
}
});
Run Code Online (Sandbox Code Playgroud)
该集合正确地从服务器获取并具有以下字段(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')); });
}
Run Code Online (Sandbox Code Playgroud)
如果需要计数,可以添加.length到方法的末尾.
使用下划线的filter方法和一般的JavaScript .length可以做到这一点.
Backbone的文档有一个例子filter,你只需要返回read equals false.
var unread = Notes.filter(function(note) {
return note.get("read") === false;
}).length;
Run Code Online (Sandbox Code Playgroud)
从我的手机提交,抱歉的简短回答
| 归档时间: |
|
| 查看次数: |
6281 次 |
| 最近记录: |