mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-12 11:45:38 +08:00
Support specifying requirejs modules for widget models
This commit is contained in:
parent
a58b215421
commit
031530da42
@ -188,13 +188,33 @@ define([
|
||||
WidgetManager.prototype._handle_comm_open = function (comm, msg) {
|
||||
// Handle when a comm is opened.
|
||||
var that = this;
|
||||
var model_id = comm.comm_id;
|
||||
|
||||
var instantiate_model = function(ModelType) {
|
||||
var model_id = comm.comm_id;
|
||||
var widget_model = new ModelType(that, model_id, comm);
|
||||
widget_model.on('comm:close', function () {
|
||||
delete that._models[model_id];
|
||||
});
|
||||
that._models[model_id] = widget_model;
|
||||
};
|
||||
|
||||
var widget_type_name = msg.content.data.model_name;
|
||||
var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
|
||||
widget_model.on('comm:close', function () {
|
||||
delete that._models[model_id];
|
||||
});
|
||||
this._models[model_id] = widget_model;
|
||||
var widget_module = msg.content.data.model_module;
|
||||
|
||||
if (widget_module) {
|
||||
// Load the module containing the widget model
|
||||
require([widget_module], function(mod) {
|
||||
if (mod[widget_type_name]) {
|
||||
instantiate_model(mod[widget_type_name]);
|
||||
} else {
|
||||
console.log("Error creating widget model: " + widget_type_name
|
||||
+ " not found in " + widget_module);
|
||||
}
|
||||
}, function(err) { console.log(err); });
|
||||
} else {
|
||||
// No module specified, load from the global models registry
|
||||
instantiate_model(WidgetManager._model_types[widget_type_name]);
|
||||
}
|
||||
};
|
||||
|
||||
// Backwards compatability.
|
||||
|
@ -98,6 +98,8 @@ class Widget(LoggingConfigurable):
|
||||
#-------------------------------------------------------------------------
|
||||
# Traits
|
||||
#-------------------------------------------------------------------------
|
||||
_model_module = Unicode(None, allow_none=True, help="""A requirejs module name
|
||||
in which to find _model_name. If empty, look in the global registry.""")
|
||||
_model_name = Unicode('WidgetModel', help="""Name of the backbone model
|
||||
registered in the front-end to create and sync this widget with.""")
|
||||
_view_module = Unicode(help="""A requirejs module in which to find _view_name.
|
||||
@ -142,7 +144,9 @@ class Widget(LoggingConfigurable):
|
||||
def open(self):
|
||||
"""Open a comm to the frontend if one isn't already open."""
|
||||
if self.comm is None:
|
||||
args = dict(target_name='ipython.widget', data={ 'model_name': self._model_name })
|
||||
args = dict(target_name='ipython.widget',
|
||||
data={'model_name': self._model_name,
|
||||
'model_module': self._model_module})
|
||||
if self._model_id is not None:
|
||||
args['comm_id'] = self._model_id
|
||||
self.comm = Comm(**args)
|
||||
|
Loading…
Reference in New Issue
Block a user