Skip to content

Instantly share code, notes, and snippets.

@CH-JesseMa
Forked from changemewtf/backboneFetch.js
Created May 8, 2014 19:04
Show Gist options
  • Save CH-JesseMa/87a0250e5e5374467b55 to your computer and use it in GitHub Desktop.
Save CH-JesseMa/87a0250e5e5374467b55 to your computer and use it in GitHub Desktop.
Backbone.Collection = {
fetch: function() {
var requestUrl;
if (isAFunction(this.url)) {
requestUrl = this.url();
} else {
requestUrl = this.url;
}
this.emptyArray(this.models);
$.ajax({
url: requestUrl,
type: "GET",
dataType: "json"
}).done(function(results){
// `results` will be a list of vanilla JSON objects
// `this.model` will be something returned by Backbone.Model.extend()
var ModelConstructor = this.model;
_.each(results, function(result){
var model = ModelConstructor(result);
this.models.push(model);
this.triggerEvent("add", model);
});
this.triggerEvent("sync");
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment