Skip to content

Commit

Permalink
Add "fetch" event on Models and Collections to be able to observe it.…
Browse files Browse the repository at this point in the history
… I found this pretty useful to add "generic" loader views on any collections/models.


Simple example, a generic loader that can be quickly binded to any collection, and will do the job.

var Loader = Backbone.View.extend({

	tagName:'img',
	attributes:{
		src:'loader.gif'
	},

	initialize:function() {
		_.bindAll(this,'hide','show')
		this.collection.on('reset',this.hide);
		this.collection.on('fetch',this.show);
	},

	hide:function() {
		$(this.el).hide();
	},
	show:function() {
		$(this.el).show();
	}

})
  • Loading branch information
spacenick committed Jul 3, 2012
1 parent d8477f4 commit 2dafb01
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backbone.js
Expand Up @@ -336,6 +336,10 @@
model.trigger('sync', model, resp, options);
};
options.error = Backbone.wrapError(options.error, model, options);

// Trigger "fetch" events
this.trigger('fetch',this,options);

return this.sync('read', this, options);
},

Expand Down Expand Up @@ -780,6 +784,10 @@
collection.trigger('sync', collection, resp, options);
};
options.error = Backbone.wrapError(options.error, collection, options);

// Trigger "fetch" events
this.trigger('fetch',this,options);

return this.sync('read', this, options);
},

Expand Down

0 comments on commit 2dafb01

Please sign in to comment.