Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

javascript - Getting started with backbonejs - what should a server return

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()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...