Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"How" to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?

I am well aware it can be done and I've looked at quite a few places (including: Best practice for saving an entire collection?). But I'm still not clear "exactly how" is it written in code? (the post explains it in English. It'd be great to have a javascript specific explanation :)

Say I have a collection of models - the models themselves may have nested collections. I have overridden the toJSON() method of the parent collection and I am getting a valid JSON object. I wish to "save" the entire collection (corresponding JSON), but backbone doesn't seem to come in-built with that functionality.

var MyCollection = Backbone.Collection.extend({ model:MyModel,  //something to save? save: function() {    //what to write here?  }  }); 

I know somewhere you have to say:

Backbone.sync = function(method, model, options){ /*  * What goes in here?? If at all anything needs to be done?  * Where to declare this in the program? And how is it called?  */ } 

Once the 'view' is done with the processing it is responsible for telling the collection to "save" itself on the server (capable of handling a bulk update/create request).

Questions that arise:

  1. How/what to write in code to "wire it all together"?
  2. What is the 'right' location of the callbacks and how to specify a "success/error" callback? I mean syntactically?I'm not clear of the syntax of registering callbacks in backbone...

If it is indeed a tricky job then can we call jQuery.ajax within a view and pass the this.successMethod or this.errorMethod as success/error callbacks?? Will it work?

I need to get in sync with backbone's way of thinking - I know I'm definitely missing something w.r.t., syncing of entire collections.

like image 834
PhD Avatar asked Jul 29 '11 21:07

PhD


People also ask

Does backbone use jQuery?

By default Backbone will use: jQuery, // Zepto, or Ender; but the `setDomLibrary()` method lets you inject an // alternate JavaScript library (or a mock library for testing your views // outside of a browser).

How do you save in backbone?

There are no save files in this game, you see. That means no save scumming, and no instant reloads when you do something ill-advised. You just… make the choices you make, and watch the consequences play out afterwards.

Why use Backbone js?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.

How does Backbone js work?

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.


2 Answers

My immediate thought is not to override the method on save method on Backbone.Collection but wrap the collection in another Backbone.Model and override the toJSON method on that. Then Backbone.js will treat the model as a single resource and you don't have to hack the way backone thinks too much.

Note that Backbone.Collection has a toJSON method so most of your work is done for you. You just have to proxy the toJSON method of your wrapper Backbone.Model to the Backbone.collection.

var MyCollectionWrapper = Backbone.Model.extend({ url: "/bulkupload",  //something to save? toJSON: function() {     return this.model.toJSON(); // where model is the collection class YOU defined above  }  }); 
like image 175
bradgonesurfing Avatar answered Oct 05 '22 06:10

bradgonesurfing


A very simple...

Backbone.Collection.prototype.save = function (options) {     Backbone.sync("create", this, options); }; 

...will give your collections a save method. Bear in mind this will always post all the collection's models to the server regardless of what has changed. options are just normal jQuery ajax options.

like image 24
hacklikecrack Avatar answered Oct 05 '22 08:10

hacklikecrack