##// END OF EJS Templates
Support specifying requirejs modules for widget models
Thomas Kluyver -
Show More
@@ -188,13 +188,33 b' define(['
188 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
188 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
189 // Handle when a comm is opened.
189 // Handle when a comm is opened.
190 var that = this;
190 var that = this;
191
192 var instantiate_model = function(ModelType) {
191 var model_id = comm.comm_id;
193 var model_id = comm.comm_id;
192 var widget_type_name = msg.content.data.model_name;
194 var widget_model = new ModelType(that, model_id, comm);
193 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
194 widget_model.on('comm:close', function () {
195 widget_model.on('comm:close', function () {
195 delete that._models[model_id];
196 delete that._models[model_id];
196 });
197 });
197 this._models[model_id] = widget_model;
198 that._models[model_id] = widget_model;
199 };
200
201 var widget_type_name = msg.content.data.model_name;
202 var widget_module = msg.content.data.model_module;
203
204 if (widget_module) {
205 // Load the module containing the widget model
206 require([widget_module], function(mod) {
207 if (mod[widget_type_name]) {
208 instantiate_model(mod[widget_type_name]);
209 } else {
210 console.log("Error creating widget model: " + widget_type_name
211 + " not found in " + widget_module);
212 }
213 }, function(err) { console.log(err); });
214 } else {
215 // No module specified, load from the global models registry
216 instantiate_model(WidgetManager._model_types[widget_type_name]);
217 }
198 };
218 };
199
219
200 // Backwards compatability.
220 // Backwards compatability.
@@ -98,6 +98,8 b' class Widget(LoggingConfigurable):'
98 #-------------------------------------------------------------------------
98 #-------------------------------------------------------------------------
99 # Traits
99 # Traits
100 #-------------------------------------------------------------------------
100 #-------------------------------------------------------------------------
101 _model_module = Unicode(None, allow_none=True, help="""A requirejs module name
102 in which to find _model_name. If empty, look in the global registry.""")
101 _model_name = Unicode('WidgetModel', help="""Name of the backbone model
103 _model_name = Unicode('WidgetModel', help="""Name of the backbone model
102 registered in the front-end to create and sync this widget with.""")
104 registered in the front-end to create and sync this widget with.""")
103 _view_module = Unicode(help="""A requirejs module in which to find _view_name.
105 _view_module = Unicode(help="""A requirejs module in which to find _view_name.
@@ -142,7 +144,9 b' class Widget(LoggingConfigurable):'
142 def open(self):
144 def open(self):
143 """Open a comm to the frontend if one isn't already open."""
145 """Open a comm to the frontend if one isn't already open."""
144 if self.comm is None:
146 if self.comm is None:
145 args = dict(target_name='ipython.widget', data={ 'model_name': self._model_name })
147 args = dict(target_name='ipython.widget',
148 data={'model_name': self._model_name,
149 'model_module': self._model_module})
146 if self._model_id is not None:
150 if self._model_id is not None:
147 args['comm_id'] = self._model_id
151 args['comm_id'] = self._model_id
148 self.comm = Comm(**args)
152 self.comm = Comm(**args)
General Comments 0
You need to be logged in to leave comments. Login now