Skip to content

Instantly share code, notes, and snippets.

@changemewtf
Created May 8, 2014 19:03
Show Gist options
  • Save changemewtf/1f2673cdc3bb523007e8 to your computer and use it in GitHub Desktop.
Save changemewtf/1f2673cdc3bb523007e8 to your computer and use it in GitHub Desktop.
Some pseudocode representing part of what Backbone.Collection.fetch() does.
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