Note that there are some explanatory texts on larger screens.

plurals
  1. PO"How" to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?
    primarykey
    data
    text
    <p>I am well aware it can be done and I've looked at quite a few places (including: <a href="https://stackoverflow.com/questions/5014216/best-practice-for-saving-an-entire-collection">Best practice for saving an entire collection?</a>). 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 :)</p> <p>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.</p> <pre><code>var MyCollection = Backbone.Collection.extend({ model:MyModel, //something to save? save: function() { //what to write here? } }); </code></pre> <p>I know somewhere you have to say:</p> <pre><code>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? */ } </code></pre> <p>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). </p> <p>Questions that arise:</p> <ol> <li>How/what to write in code to "wire it all together"?</li> <li>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...</li> </ol> <p>If it is indeed a tricky job then can we call jQuery.ajax within a view and pass the <code>this.successMethod</code> or <code>this.errorMethod</code> as success/error callbacks?? Will it work?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload