daily pastebin goal
96%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Lazily load models via XHR in MVC-style JavaScript
  2.  
  3. var Model = function(params) {};
  4. Model.load_and_retry = function(id, controller, controller_variable) {
  5.   var controller_method = this.load_and_retry.caller;
  6.   var data_loaded_callback = function(data) {
  7.     controller[controller_variable] = new Model(data);
  8.     controller_method.apply(controller);
  9.   };
  10.  
  11.   load_with_xhr("some/url/"+id+".json", data_loaded_callback);
  12. };
  13.  
  14.  
  15. // Example
  16.  
  17. var Controller = function() {};
  18. Controller.prototype.do_something = function() {
  19.   if(!this.my_model) {
  20.     return Model.load_and_retry(this.model_id, this, "my_model");
  21.   }
  22.   alert(this.my_model);
  23. }
RAW Paste Data
Top