Skip to content

Instantly share code, notes, and snippets.

@georgebrock
Created September 21, 2009 13:03
Show Gist options
  • Save georgebrock/190233 to your computer and use it in GitHub Desktop.
Save georgebrock/190233 to your computer and use it in GitHub Desktop.
// Lazily load models via XHR in MVC-style JavaScript
var Model = function(params) {};
Model.load_and_retry = function(id, controller, controller_variable) {
var controller_method = this.load_and_retry.caller;
var data_loaded_callback = function(data) {
controller[controller_variable] = new Model(data);
controller_method.apply(controller);
};
load_with_xhr("some/url/"+id+".json", data_loaded_callback);
};
// Example
var Controller = function() {};
Controller.prototype.do_something = function() {
if(!this.my_model) {
return Model.load_and_retry(this.model_id, this, "my_model");
}
alert(this.my_model);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment