Created
February 22, 2013 12:51
-
-
Save sivagao/5013195 to your computer and use it in GitHub Desktop.
Backbone: to indicate Backbone fetch progress , to override a method @ Collection, Model . //patching the fetch method on Collection.prototype to emit a fetch event.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Repos.Views.List = Backbone.View.extend({ | |
| initialize: function() { | |
| // Display a loading indication whenever the Collection is fetching. | |
| this.collection.on("fetch", function() { | |
| this.html("<img src='/assets/img/spinner.gif'>"); | |
| }, this); | |
| // Automatically re-render whenever the Collection is populated. | |
| this.collection.on("reset", this.render, this); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Patch Model and Collection. | |
| _.each(["Model", "Collection"], function(name) { | |
| // Cache Backbone constructor. | |
| var ctor = Backbone[name]; | |
| // Cache original fetch. | |
| var fetch = ctor.prototype.fetch; | |
| // Override the fetch method to emit a fetch event. | |
| ctor.prototype.fetch = function() { | |
| // Trigger the fetch event on the instance. | |
| this.trigger("fetch", this); | |
| // Pass through to original fetch. | |
| return fetch.apply(this, arguments); | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For future readers, there's now a
requestevent which replaces thefetchevent here. And there's asyncevent which is triggered when a model or collection has been successfully synced with the server.