Support specifying requirejs modules for widget models

This commit is contained in:
Thomas Kluyver 2014-10-21 10:57:10 -07:00
parent a58b215421
commit 031530da42
2 changed files with 31 additions and 7 deletions

View File

@ -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.

View File

@ -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)