[英]In Backbone.js, after I 'create' a model under a collection, how do I update that model?
Posts.create({'body':post_body});
When I call that, Backbone will hit my sever with an AJAX post request, creating that post. 当我打电话给Backbone时,它将用AJAX帖子请求来打我的服务器,从而创建该帖子。 My server will then return a JSON with the "full" post.
然后,我的服务器将返回带有“ full”帖子的JSON。
Perfect! 完善! But now, I want the newly created model to to have the full data.
但是现在,我希望新创建的模型具有完整的数据。 In other words, I don't want it to only have the body attribute.
换句话说,我不希望它仅具有body属性。 (all my other models have other data).
(我所有其他模型都有其他数据)。
My question is: 我的问题是:
Edit: I did this, and it seems like Backbone automatically uses the data returned as the new model. 编辑:我这样做了,并且看起来Backbone会自动使用返回的数据作为新模型。 Can someone confirm?
有人可以确认吗?
success:function(post){
console.log(post.toJSON()); //Yay! latest version.
},
Yes, the model will be updated with any additional info your server returns. 是的,将使用服务器返回的所有其他信息来更新模型。
This is because under the model's create
method, there is a call to save
, which intern calls model.set
within it, in order to update the model with any amended (or new attributes). 这是因为在模型的
create
方法下,有一个调用save
,该操作model.set
内部调用了model.set
,以便使用任何已修改的(或新属性)更新模型。
As the source code comment for this method states: 如该方法的源代码注释所述:
If the server returns an attributes hash that differs, the model's state will be set again.
如果服务器返回的属性哈希不同,则将再次设置模型的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.