Show More
@@ -188,13 +188,33 b' define([' | |||
|
188 | 188 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
189 | 189 | // Handle when a comm is opened. |
|
190 | 190 | var that = this; |
|
191 | var model_id = comm.comm_id; | |
|
191 | ||
|
192 | var instantiate_model = function(ModelType) { | |
|
193 | var model_id = comm.comm_id; | |
|
194 | var widget_model = new ModelType(that, model_id, comm); | |
|
195 | widget_model.on('comm:close', function () { | |
|
196 | delete that._models[model_id]; | |
|
197 | }); | |
|
198 | that._models[model_id] = widget_model; | |
|
199 | }; | |
|
200 | ||
|
192 | 201 | var widget_type_name = msg.content.data.model_name; |
|
193 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); | |
|
194 | widget_model.on('comm:close', function () { | |
|
195 | delete that._models[model_id]; | |
|
196 | }); | |
|
197 | this._models[model_id] = widget_model; | |
|
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 | 220 | // Backwards compatability. |
@@ -98,6 +98,8 b' class Widget(LoggingConfigurable):' | |||
|
98 | 98 | #------------------------------------------------------------------------- |
|
99 | 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 | 103 | _model_name = Unicode('WidgetModel', help="""Name of the backbone model |
|
102 | 104 | registered in the front-end to create and sync this widget with.""") |
|
103 | 105 | _view_module = Unicode(help="""A requirejs module in which to find _view_name. |
@@ -142,7 +144,9 b' class Widget(LoggingConfigurable):' | |||
|
142 | 144 | def open(self): |
|
143 | 145 | """Open a comm to the frontend if one isn't already open.""" |
|
144 | 146 | if self.comm is None: |
|
145 |
args = dict(target_name='ipython.widget', |
|
|
147 | args = dict(target_name='ipython.widget', | |
|
148 | data={'model_name': self._model_name, | |
|
149 | 'model_module': self._model_module}) | |
|
146 | 150 | if self._model_id is not None: |
|
147 | 151 | args['comm_id'] = self._model_id |
|
148 | 152 | self.comm = Comm(**args) |
General Comments 0
You need to be logged in to leave comments.
Login now