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