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"
});
  1. What JSON should server return assuming validation went well for model.save()

  2. What JSON should server return for model.fetch()

  3. 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"
});
  1. What JSON should server return assuming validation went well for model.save()

  2. What JSON should server return for model.fetch()

  3. What JSON should server return for model.destroy()

Share Improve this question asked Mar 14, 2012 at 16:45 DeeptechtonsDeeptechtons 11.1k27 gold badges105 silver badges178 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

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:

  1. 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]' }

  2. Fetch should just return the model in JSON form. So, the same example I used for model.save would work.

  3. 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