Created
September 21, 2009 13:03
-
-
Save georgebrock/190233 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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