Question

I am very much new to backbone.js; What I understood from tutorials is that, backbone is tightly coupled with database ; which is in the form of models and collections on client side.

Assume that, if your site is holding up per-existing RESTFUL services to delete/Add/update records base on the params.

My Query is , can I still use Backbone since what I am assuming is, once user is saving or updating model which needs to be persisted on server , is handled by Save() right?

so If I don't have a control on server to write POST actions and if custom services are handling this stuff instead ; can I still use backbone by calling REST service to update record.?

Please guide.

Was it helpful?

Solution

It depends on how the server is set up, to some degree. Backbone is designed to work with RESTful services. I think you already looked through it, but for completeness here is the documentation for save: http://backbonejs.org/#Model-save

save and fetch and destroy delegate to Backbone.sync: http://backbonejs.org/#Sync

And AJAX queries are sent by default using jQuery.ajax: http://api.jquery.com/jQuery.ajax/ or, if you can't use jQuery, Backbone.ajax: http://backbonejs.org/#Sync-ajax

You want to make sure the parameters you are sending match what the server is looking for (that the keys and values you need match up), and that it can accept a JSON object, which should be a normal thing. And then you would want models to be returned as JSON objects.

But even if the server doesn't work that way, you can customize everything in Backbone to match what you need. You can customize the ajax request, you can customize the toJSON method which preps the model or collection to be sent via AJAX (docs here), you can customize the parse method which handles the response from the server (docs here).

The defaults in Backbone are very simple and the server doesn't have to do much to work with it, but you can easily basically rewrite how Backbone works if you need to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top