Skip to content

Instantly share code, notes, and snippets.

@sumitngupta
Created July 23, 2012 19:10
Show Gist options
  • Select an option

  • Save sumitngupta/3165516 to your computer and use it in GitHub Desktop.

Select an option

Save sumitngupta/3165516 to your computer and use it in GitHub Desktop.
// 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);
this._lastFetchedAt = new Date();
// Pass through to original fetch.
return fetch.apply(this, arguments);
};
ctor.prototype.hasFetched = function () {
return !!this._lastFetchedAt;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment