Backbone.js 没有在模型更改时发布到服务器?为什么?
我有以下内容:
// MODEL
NotificationModel = C.BB.Model.extend({
defaults : {
read : false
},
urlRoot : '/notifications'
});
// COLLECTION
NotificationCollection = C.BB.Collection.extend({
model: NotificationModel,
url: '/notifications',
initialize : function() {
var me = this;
me.fetch();
}
});
稍后在视图中我有:
....
onCollectionAdd : function(m,c,cfg) {
m.set({read: true},{silent: false});
该集正在更改项目值,但 Backbone 不会将更新发布到服务器。有什么建议吗?谢谢
I have the following:
// MODEL
NotificationModel = C.BB.Model.extend({
defaults : {
read : false
},
urlRoot : '/notifications'
});
// COLLECTION
NotificationCollection = C.BB.Collection.extend({
model: NotificationModel,
url: '/notifications',
initialize : function() {
var me = this;
me.fetch();
}
});
Later in the view I have:
....
onCollectionAdd : function(m,c,cfg) {
m.set({read: true},{silent: false});
The set is changing the items value but Backbone is not posting the update to the server. Any suggestions? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要调用m.save(),它会调用相应的方法create()或update()。
set()方法只触发change()事件,后台不保存任何数据
You need to call m.save(), which will call correspondent method create() or update().
Method set() trigger only change() event, which not save any data in backend
您可以将事件绑定到集合来保存它们:
请注意,还有
Collection。 create
方法将新模型添加到集合中并将集合保存在服务器上。You could bind an event to the collection to save them:
Note there is also the
Collection.create
method wich add a new model to the collection and save the collection on the server.