admin管理员组文章数量:1289901
I am totally new to Backbone.js library and read through the whole documentation and understood the working of the library. In the below cases what should the response from the server be for proper working of application designed using backbone (without putting a extra stroke/code).
assume a model like below
window.person = Backbone.Model.extend({
defaults: {
name: "",
email: "[email protected]"
},
urlRoot: "PersonApp"
});
What JSON should server return assuming validation went well for
model.save()
What JSON should server return for
model.fetch()
What JSON should server return for
model.destroy()
I am totally new to Backbone.js library and read through the whole documentation and understood the working of the library. In the below cases what should the response from the server be for proper working of application designed using backbone (without putting a extra stroke/code).
assume a model like below
window.person = Backbone.Model.extend({
defaults: {
name: "",
email: "[email protected]"
},
urlRoot: "PersonApp"
});
What JSON should server return assuming validation went well for
model.save()
What JSON should server return for
model.fetch()
What JSON should server return for
model.destroy()
1 Answer
Reset to default 15If you have a look in the Backbone.Sync documentation, it says that you should respond to requests with the attributes that have changed on the server.
So to answer your questions:
The JSON request for
model.save
should return the attributes that have changed as part of the save. In the case of a create this would be the whole model; in the case of update just the fields that have changed. (Or if you're lazy and don't mind updating the entire client side model, you could just return the whole model).So an acceptable response would be
{ 'name' : 'a name', 'email' : '[email protected]' }
Fetch should just return the model in JSON form. So, the same example I used for
model.save
would work.I'm not entirely sure, but I don't think Backbone validates the returned data from delete requests so you should be able to return anything, so long as it's not an HTTP error. According to @a.real.human.being below, an empty response also causes errors. So returning a 200 with "OK" in the body (or similar) seems like a reasonable plan.
本文标签: javascriptGetting started with backbonejswhat should a server returnStack Overflow
版权声明:本文标题:javascript - Getting started with backbonejs - what should a server return - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741413064a2377351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论