Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 817
Deeptechtons Avatar asked Mar 14 '12 16:03

Deeptechtons


People also ask

What is backbonejs?

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.

What is the use of backbone in backend?

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.

How do I use backbone events?

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.

Why use backbone for rich client-side applications?

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.


1 Answers

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.

like image 130
obmarg Avatar answered Oct 23 '22 11:10

obmarg