Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating a collection using fetch with backbone
    primarykey
    data
    text
    <p>Based on the official documentation, when I do something like this:</p> <pre><code>collection.fetch({update: true, remove: false}) </code></pre> <p>I get an "add" event for every new model, and a "change" event for every changed existing model, without removing anything.</p> <p>Why if I'm calling an static source of data (the url of the collection always return the same json) an add event is called for each item received?</p> <p>Here some code (I'm not rendering anything, I'm just debugging):</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Example&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="#refresh"&gt;Refresh&lt;/a&gt; &lt;script src="js/jquery-1.8.3.min.js"&gt;&lt;/script&gt; &lt;script src="js/underscore-min.js"&gt;&lt;/script&gt; &lt;script src="js/backbone-min.js"&gt;&lt;/script&gt; &lt;script src="js/main.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Heres the JS</p> <pre><code>(function($){ //Twitter Model ModelsTwitt = Backbone.Model.extend({}); //Twitter Collection CollectionsTwitts = Backbone.Collection.extend({ model:ModelsTwitt, initialize:function(){ console.log('Twitter App Started'); }, url:'data/195.json' }); //Twitts View ViewsTwitts = Backbone.View.extend({ el:$('#twitter-list'), initialize:function(){ _.bindAll(this, 'render'); this.collection.bind('reset',this.render); this.collection.bind('add',this.add); }, render:function(){ console.log("This is the collection",this.collection); }, add:function(model){ console.log("add event called",model); } }); //Twitter Router Router = Backbone.Router.extend({ routes:{ '':'defaultRoute',//Default list twitts route 'refresh':'refreshView' }, defaultRoute:function(){ this.twitts = new CollectionsTwitts(); new ViewsTwitts({collection:this.twitts}); this.twitts.fetch(); }, refreshView:function(){ this.twitts.fetch({update:true,remove:false}); } }); var appRouter = new Router(); Backbone.history.start(); })(jQuery); </code></pre> <p>Basically, I fetch the collection using the defaultroute, it is fetched properly with all the models and attributes.</p> <p>And when I click on the refresh link, I call refreshView, which basically tries to update the collection with new models. What I don't understand is why if the response is the same, all the models of the collection are detected as new, triggering an <em>add</em>?</p> <p><a href="https://googledrive.com/host/0B2e89I7TTTCjdnhwaEZtdkpzZnc/index.html" rel="noreferrer">Heres a functional link:</a> open the console and you will see how the add events are called when you click on refresh even when the collection is the same.</p> <p>Thanks for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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