如何使用 Backbone.js 更新一组记录铁轨?
我的 Rails & 中有通知Backbone.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();
}
});
该模型具有以下字段(id、read),其中 read 为 true 或 false。
我想要做的是,一旦用户查看通知,在服务器上将所有通知标记为 READ = true。使用 Backbone.js 执行此操作的正确方法是什么?
谢谢
I have notifications in my Rails & Backbone.js app
// MODEL
NotificationModel = App.BB.Model.extend({
defaults : {}
});
// COLLECTION
NotificationCollection = App.BB.Collection.extend({
model: NotificationModel,
url: '/notifications',
initialize : function() {
var me = this;
me.fetch();
}
});
The model has the following fields (id, read) where read is true or false.
What I want to do is once the user views the notifications, mark all as READ = true on the server. What's the right way to do that with Backbone.js?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Backbone.js 中没有批量上传
所以你有两个选择
继续工作,因为骨干做得最好,
您可以更新收藏中的每个模型,以将项目设置为已读。
这会将模型发布到服务器并更新您的服务器(仅适用于该模型)
是的,这可能看起来很重,但骨干网以这种方式工作,没有批量更新模型(还?)
另一个解决方案是使用自定义的 Ajax 调用自己发布它,
例如这个 jQuery 调用。
请记住:在此 ajax 调用完成后,您可能需要再次获取集合来更新它(否则集合中的模型将不知道您对服务器所做的更改,因此所有模型将列出 < code>read: true 在他们的changedAttributes中。
There is no Bulk upload in Backbone.js
so you have two choices
continue to work as backbone does best,
you can update every model in your collection to set the item as read.
this will post the model to the server and update your server (for that model only)
yes, this might seem heavy but backbone works this way, there is no bulk update of models (yet?)
the other solution is to post it yourself usign a custom made Ajax call,
for example this jQuery call.
keep in mind that: after this ajax call is complete you might want to fetch your collection again to update it (otherwise the models in your collection will don't know about the changes yo u did to the server, so all models will list
read: true
in their changedAttributes.