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()
TL;DR: BackboneJS is a JavaScript library that provides models with key-value bindings and custom events, views with declarative event handling, and collections with an abundant API of enumerable functions. Currently, BackboneJS has over 25,000 stars on GitHub.
Backbone.js Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
Backbone.Events is designed so that you can mix it in to any JavaScript object or prototype. Since you can use any string as an event, it's often handy to bind and trigger your own custom events: model.on ("selected:true") or model.on ("editing") Render the UI as you see fit.
For rich client-side applications, a more structured approach is often helpful. With Backbone, you represent your data as Models, which can be created, validated, destroyed, and saved to the server.
If 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With