##// END OF EJS Templates
Simplify code by using Promises in a better way; try_load -> load
Jason Grout -
Show More
@@ -606,7 +606,7 b' define(['
606 });
606 });
607 };
607 };
608
608
609 var try_load = function(class_name, module_name, registry) {
609 var load = function(class_name, module_name, registry) {
610 // Tries to load a class
610 // Tries to load a class
611 //
611 //
612 // Tries to load a class from a module using require.js, if a module
612 // Tries to load a class from a module using require.js, if a module
@@ -618,7 +618,7 b' define(['
618 if (module_name) {
618 if (module_name) {
619 require([module_name], function(module) {
619 require([module_name], function(module) {
620 if (module[class_name] === undefined) {
620 if (module[class_name] === undefined) {
621 reject(Error('Class not found in module.'));
621 reject(new Error('Class not found in module.'));
622 } else {
622 } else {
623 resolve(module[class_name]);
623 resolve(module[class_name]);
624 }
624 }
@@ -627,12 +627,12 b' define(['
627 if (registry && registry[class_name]) {
627 if (registry && registry[class_name]) {
628 resolve(registry[class_name]);
628 resolve(registry[class_name]);
629 } else {
629 } else {
630 reject(Error('Class not found in registry.'));
630 reject(new Error('Class not found in registry.'));
631 }
631 }
632 }
632 }
633 });
633 });
634 };
634 };
635
635
636 var utils = {
636 var utils = {
637 regex_split : regex_split,
637 regex_split : regex_split,
638 uuid : uuid,
638 uuid : uuid,
@@ -662,7 +662,7 b' define(['
662 XHR_ERROR : XHR_ERROR,
662 XHR_ERROR : XHR_ERROR,
663 wrap_ajax_error : wrap_ajax_error,
663 wrap_ajax_error : wrap_ajax_error,
664 promising_ajax : promising_ajax,
664 promising_ajax : promising_ajax,
665 try_load: try_load,
665 load: load,
666 };
666 };
667
667
668 // Backwards compatability.
668 // Backwards compatability.
@@ -87,27 +87,22 b' define(['
87
87
88
88
89 WidgetManager.prototype.create_view = function(model, options) {
89 WidgetManager.prototype.create_view = function(model, options) {
90 // Creates a view for a particular model.
90 // Creates a promise for a view of a given model
91 return new Promise(function(resolve, reject) {
91 return utils.load(model.get('_view_name'), model.get('_view_module'),
92 var view_name = model.get('_view_name');
92 WidgetManager._view_types).then(function(ViewType) {
93 var view_module = model.get('_view_module');
93 // If a view is passed into the method, use that view's cell as
94 utils.try_load(view_name, view_module, WidgetManager._view_types).then(function(ViewType){
94 // the cell for the view that is created.
95
95 options = options || {};
96 // If a view is passed into the method, use that view's cell as
96 if (options.parent !== undefined) {
97 // the cell for the view that is created.
97 options.cell = options.parent.options.cell;
98 options = options || {};
98 }
99 if (options.parent !== undefined) {
99 // Create and render the view...
100 options.cell = options.parent.options.cell;
100 var parameters = {model: model, options: options};
101 }
101 var view = new ViewType(parameters);
102
102 view.listenTo(model, 'destroy', view.remove);
103 // Create and render the view...
103 view.render();
104 var parameters = {model: model, options: options};
104 return view;
105 var view = new ViewType(parameters);
105 });
106 view.render();
107 model.on('destroy', view.remove, view);
108 resolve(view);
109 }, reject);
110 });
111 };
106 };
112
107
113 WidgetManager.prototype.get_msg_cell = function (msg_id) {
108 WidgetManager.prototype.get_msg_cell = function (msg_id) {
@@ -171,20 +166,7 b' define(['
171 };
166 };
172
167
173 WidgetManager.prototype.get_model = function (model_id) {
168 WidgetManager.prototype.get_model = function (model_id) {
174 // Look-up a model instance by its id.
169 return that._models[model_id];
175 var that = this;
176 var model = that._models[model_id];
177 if (model !== undefined) {
178 return new Promise(function(resolve, reject){
179 if (model instanceof Promise) {
180 model.then(resolve, reject);
181 } else {
182 resolve(model);
183 }
184 });
185 } else {
186 return undefined;
187 }
188 };
170 };
189
171
190 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
172 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
@@ -196,7 +178,7 b' define(['
196 };
178 };
197
179
198 WidgetManager.prototype.create_model = function (options) {
180 WidgetManager.prototype.create_model = function (options) {
199 // Create and return a promise to create a new widget model.
181 // Create and return a promise for a new widget model
200 //
182 //
201 // Minimally, one must provide the model_name and widget_class
183 // Minimally, one must provide the model_name and widget_class
202 // parameters to create a model from Javascript.
184 // parameters to create a model from Javascript.
@@ -230,23 +212,16 b' define(['
230
212
231 var that = this;
213 var that = this;
232 var model_id = comm.comm_id;
214 var model_id = comm.comm_id;
233 var promise = new Promise(function(resolve, reject) {
215 var model_promise = utils.load(options.model_name, options.model_module, WidgetManager._model_types)
234
216 .then(function(ModelType) {
235 // Get the model type using require or through the registry.
217 var widget_model = new ModelType(that, model_id, comm);
236 var widget_type_name = options.model_name;
218 widget_model.once('comm:close', function () {
237 var widget_module = options.model_module;
219 delete that._models[model_id];
238 utils.try_load(widget_type_name, widget_module, WidgetManager._model_types)
220 });
239 .then(function(ModelType) {
221 return widget_model;
240 var widget_model = new ModelType(that, model_id, comm);
222 });
241 widget_model.on('comm:close', function () {
223 this._models[model_id] = model_promise;
242 delete that._models[model_id];
224 return model_promise;
243 });
244 that._models[model_id] = widget_model;
245 resolve(widget_model);
246 }, reject);
247 });
248 this._models[model_id] = promise;
249 return promise;
250 };
225 };
251
226
252 // Backwards compatibility.
227 // Backwards compatibility.
General Comments 0
You need to be logged in to leave comments. Login now