mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-30 12:11:32 +08:00
Simplify code by using Promises in a better way; try_load -> load
This commit is contained in:
parent
c41dcb35e9
commit
4412c12929
@ -606,7 +606,7 @@ define([
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var try_load = function(class_name, module_name, registry) {
|
var load = function(class_name, module_name, registry) {
|
||||||
// Tries to load a class
|
// Tries to load a class
|
||||||
//
|
//
|
||||||
// Tries to load a class from a module using require.js, if a module
|
// Tries to load a class from a module using require.js, if a module
|
||||||
@ -618,7 +618,7 @@ define([
|
|||||||
if (module_name) {
|
if (module_name) {
|
||||||
require([module_name], function(module) {
|
require([module_name], function(module) {
|
||||||
if (module[class_name] === undefined) {
|
if (module[class_name] === undefined) {
|
||||||
reject(Error('Class not found in module.'));
|
reject(new Error('Class not found in module.'));
|
||||||
} else {
|
} else {
|
||||||
resolve(module[class_name]);
|
resolve(module[class_name]);
|
||||||
}
|
}
|
||||||
@ -627,7 +627,7 @@ define([
|
|||||||
if (registry && registry[class_name]) {
|
if (registry && registry[class_name]) {
|
||||||
resolve(registry[class_name]);
|
resolve(registry[class_name]);
|
||||||
} else {
|
} else {
|
||||||
reject(Error('Class not found in registry.'));
|
reject(new Error('Class not found in registry.'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -662,7 +662,7 @@ define([
|
|||||||
XHR_ERROR : XHR_ERROR,
|
XHR_ERROR : XHR_ERROR,
|
||||||
wrap_ajax_error : wrap_ajax_error,
|
wrap_ajax_error : wrap_ajax_error,
|
||||||
promising_ajax : promising_ajax,
|
promising_ajax : promising_ajax,
|
||||||
try_load: try_load,
|
load: load,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Backwards compatability.
|
// Backwards compatability.
|
||||||
|
@ -87,26 +87,21 @@ define([
|
|||||||
|
|
||||||
|
|
||||||
WidgetManager.prototype.create_view = function(model, options) {
|
WidgetManager.prototype.create_view = function(model, options) {
|
||||||
// Creates a view for a particular model.
|
// Creates a promise for a view of a given model
|
||||||
return new Promise(function(resolve, reject) {
|
return utils.load(model.get('_view_name'), model.get('_view_module'),
|
||||||
var view_name = model.get('_view_name');
|
WidgetManager._view_types).then(function(ViewType) {
|
||||||
var view_module = model.get('_view_module');
|
|
||||||
utils.try_load(view_name, view_module, WidgetManager._view_types).then(function(ViewType){
|
|
||||||
|
|
||||||
// If a view is passed into the method, use that view's cell as
|
// If a view is passed into the method, use that view's cell as
|
||||||
// the cell for the view that is created.
|
// the cell for the view that is created.
|
||||||
options = options || {};
|
options = options || {};
|
||||||
if (options.parent !== undefined) {
|
if (options.parent !== undefined) {
|
||||||
options.cell = options.parent.options.cell;
|
options.cell = options.parent.options.cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and render the view...
|
// Create and render the view...
|
||||||
var parameters = {model: model, options: options};
|
var parameters = {model: model, options: options};
|
||||||
var view = new ViewType(parameters);
|
var view = new ViewType(parameters);
|
||||||
|
view.listenTo(model, 'destroy', view.remove);
|
||||||
view.render();
|
view.render();
|
||||||
model.on('destroy', view.remove, view);
|
return view;
|
||||||
resolve(view);
|
|
||||||
}, reject);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -171,20 +166,7 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
WidgetManager.prototype.get_model = function (model_id) {
|
WidgetManager.prototype.get_model = function (model_id) {
|
||||||
// Look-up a model instance by its id.
|
return that._models[model_id];
|
||||||
var that = this;
|
|
||||||
var model = that._models[model_id];
|
|
||||||
if (model !== undefined) {
|
|
||||||
return new Promise(function(resolve, reject){
|
|
||||||
if (model instanceof Promise) {
|
|
||||||
model.then(resolve, reject);
|
|
||||||
} else {
|
|
||||||
resolve(model);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
WidgetManager.prototype._handle_comm_open = function (comm, msg) {
|
WidgetManager.prototype._handle_comm_open = function (comm, msg) {
|
||||||
@ -196,7 +178,7 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
WidgetManager.prototype.create_model = function (options) {
|
WidgetManager.prototype.create_model = function (options) {
|
||||||
// Create and return a promise to create a new widget model.
|
// Create and return a promise for a new widget model
|
||||||
//
|
//
|
||||||
// Minimally, one must provide the model_name and widget_class
|
// Minimally, one must provide the model_name and widget_class
|
||||||
// parameters to create a model from Javascript.
|
// parameters to create a model from Javascript.
|
||||||
@ -230,23 +212,16 @@ define([
|
|||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
var model_id = comm.comm_id;
|
var model_id = comm.comm_id;
|
||||||
var promise = new Promise(function(resolve, reject) {
|
var model_promise = utils.load(options.model_name, options.model_module, WidgetManager._model_types)
|
||||||
|
|
||||||
// Get the model type using require or through the registry.
|
|
||||||
var widget_type_name = options.model_name;
|
|
||||||
var widget_module = options.model_module;
|
|
||||||
utils.try_load(widget_type_name, widget_module, WidgetManager._model_types)
|
|
||||||
.then(function(ModelType) {
|
.then(function(ModelType) {
|
||||||
var widget_model = new ModelType(that, model_id, comm);
|
var widget_model = new ModelType(that, model_id, comm);
|
||||||
widget_model.on('comm:close', function () {
|
widget_model.once('comm:close', function () {
|
||||||
delete that._models[model_id];
|
delete that._models[model_id];
|
||||||
});
|
});
|
||||||
that._models[model_id] = widget_model;
|
return widget_model;
|
||||||
resolve(widget_model);
|
|
||||||
}, reject);
|
|
||||||
});
|
});
|
||||||
this._models[model_id] = promise;
|
this._models[model_id] = model_promise;
|
||||||
return promise;
|
return model_promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Backwards compatibility.
|
// Backwards compatibility.
|
||||||
|
Loading…
Reference in New Issue
Block a user