Skip to main content

Backbone Collections do not emit 'reset' event after fetch

I recently upgraded my Backbone application to the 1.0 version of the library. When I ran the application, the Backbone View was not rendering anything on the browser screen. I noticed the XHR request being sent in the Network tab of Chrome Dev Tools. This meant that the Collection was fetching the data from the server. However, the 'render' function defined in Backbone View was not being called, which was attached as a callback handler for the 'reset' event on the Collection.

Here's the sample code:

I had a quick look at the Backbone documentation and came across the section 'Upgrading to 1.0'. The very first point there mentions that the 'reset' event is not triggered by default when the Collection is populated with data. One has to pass the key - 'reset' with value true when making a fetch call on the Collection. Now I had to update the code at places where the fetch call was being made. However, this can be avoided by making changes in the Collection. There are two ways of doing this: overriding the fetch method and resetting the collection in parse method. Let's see both of them:

Overriding the fetch method: 

The fetch method is overridden here and as you can see from the above code, the 'reset' key is added to options and then the fetch method defined on the Backbone Collection's prototype is called.
Resetting the Collection in parse:

Whenever a response is received from the server, the parse method is called before sending a notification to the listeners. Here you can call the reset method passing the response as an argument. The Collection now has been reset with the data received from the server and View will receive a reset event.


  1. great, i am stucked with this problem.

  2. @Opeyemi Obembe , the approach that I've mentioned here assumes that all the fetch calls should reset the collection. If you want this to happen only in a particular instance then 'reset': true, should do the job.

  3. I was stuck on this for an hour, thanks a lot!!! I'd buy you a beer if I could.

  4. @Jashua Gupta, glad that this post helped. Beer? Sure :) You can also see my wishlist here -

  5. Haha, nice! Do you have an Amazon account? If so, I'll send you a Kindle version of one of these. I read other posts on your blog, and it's a great resource for Backbone beginners! I especially found the one on mvvm with stickit very helpful. Kudos and many thanks!


Post a Comment

Popular posts from this blog

A cheat sheet of keyboard shortcuts in ColdFusion Builder

In my last post I have explained about keyboard shortcuts in ColdFusion Builder 2.0. This blog post contains a list of all shortcut keys and I have listed these keyboard shortcuts based on the categories that it falls into. Well, this post was not planned since users can easily get to know the keyboard shortcuts by navigating to the preferences (ColdFusion -> Profiles -> Keys). However, I met Joshua at Scotch on the rocks in Edinburgh and he suggested that it would be nice to have the list of keyboard shortcuts handy. So this post is for those who would like to have the list with them and refer it whenever required.

Keyboard shortcuts for inserting text:

These are the keyboard shortcuts which are used to insert some text into the editor:

CommandKeyboard shortcuts on WindowsKeyboard shortcuts on MacInsert anchor tagCTRL + T, LCMD + T, LInsert bold tagCTRL + T, BCMD + T, BInsert br tagCTRL + T, RCMD + T, RInsert cfabortCTRL + T, ACMD + T, AInsert cfdumpCTRL + T, DCMD + T, DInsert cfs…

Custom validation messages for HTML5 Input elements using the constraint validation API

HTML5 has introduced several input types such as EMAIL, URL, RANGE, SEARCH, DATE, TIME, etc,. Most of the modern browsers have implemented them and are ready to be used in a HTML document. Another exciting feature introduced in HTML5 is the form validation. Instead of writing JavaScript to validate users input, browsers can now validate it and show an appropriate message if the validation fails. The validation message is shown in line with the field for which the validation has failed. The default error message is shown when the validation fails. In this post I'll explain how these error messages can be changed.

De-obfuscating javascript code in Chrome Developer Tools

I had blogged about JavaScript debugging with Chrome Developer Tools some time back, wherein I have explained how these developer tools can help in debugging javascript code. Today Google Chrome 12 was released and my Chrome browser was updated to this version. As with every release, there have been some improvements made on performance, usability etc,. One feature that stood out for me is the ability to De-obfuscate the javascript code.

What is Minification?

Minification is the process of removing unnecessary characters such as white spaces, comments, new lines from the source code. These otherwise would be added to make the code more readable. Minifying the source code helps in reducing the file size and thereby reducing the time taken to download the file. This is the reason why most of the popular javascript libraries such as jQuery are minified. A minified jQuery file is of 31 KB in size where as an uncompressed one is about 229 KB. Unfortunately, debugging minified javascript file…