Show More
@@ -190,7 +190,7 define([ | |||
|
190 | 190 | return this.create_model({model_name: msg.content.data.target_name, comm: comm}); |
|
191 | 191 | }; |
|
192 | 192 | |
|
193 | WidgetManager.prototype.create_model = function (model_name, target_name) { | |
|
193 | WidgetManager.prototype.create_model = function (model_name, target_name, init_state_callback) { | |
|
194 | 194 | // Create and return a new widget model. |
|
195 | 195 | // |
|
196 | 196 | // Parameters |
@@ -199,7 +199,13 define([ | |||
|
199 | 199 | // Target name of the widget model to create. |
|
200 | 200 | // target_name: string |
|
201 | 201 | // Target name of the widget in the back-end. |
|
202 | return this._create_model({model_name: model_name, target_name: target_name}); | |
|
202 | // init_state_callback: (optional) callback | |
|
203 | // Called when the first state push from the back-end is | |
|
204 | // recieved. | |
|
205 | return this._create_model({ | |
|
206 | model_name: model_name, | |
|
207 | target_name: target_name, | |
|
208 | init_state_callback: init_state_callback}); | |
|
203 | 209 | }; |
|
204 | 210 | |
|
205 | 211 | WidgetManager.prototype._create_model = function (options) { |
@@ -214,6 +220,9 define([ | |||
|
214 | 220 | // target_name: (optional) string |
|
215 | 221 | // Target name of the widget in the back-end. |
|
216 | 222 | // comm: (optional) Comm |
|
223 | // init_state_callback: (optional) callback | |
|
224 | // Called when the first state push from the back-end is | |
|
225 | // recieved. | |
|
217 | 226 | |
|
218 | 227 | // Create a comm if it wasn't provided. |
|
219 | 228 | var comm = options.comm; |
@@ -226,8 +235,8 define([ | |||
|
226 | 235 | |
|
227 | 236 | var instantiate_model = function(ModelType) { |
|
228 | 237 | var model_id = comm.comm_id; |
|
229 | var widget_model = new ModelType(that, model_id, comm); | |
|
230 |
widget_model.on('comm:close', function () { |
|
|
238 | var widget_model = new ModelType(that, model_id, comm, options.init_state_callback); | |
|
239 | widget_model.on('comm:close', function () { | |
|
231 | 240 | delete that._models[model_id]; |
|
232 | 241 | }); |
|
233 | 242 | that._models[model_id] = widget_model; |
@@ -9,7 +9,7 define(["widgets/js/manager", | |||
|
9 | 9 | ], function(widgetmanager, _, Backbone, $, IPython){ |
|
10 | 10 | |
|
11 | 11 | var WidgetModel = Backbone.Model.extend({ |
|
12 | constructor: function (widget_manager, model_id, comm) { | |
|
12 | constructor: function (widget_manager, model_id, comm, init_state_callback) { | |
|
13 | 13 | // Constructor |
|
14 | 14 | // |
|
15 | 15 | // Creates a WidgetModel instance. |
@@ -20,7 +20,11 define(["widgets/js/manager", | |||
|
20 | 20 | // model_id : string |
|
21 | 21 | // An ID unique to this model. |
|
22 | 22 | // comm : Comm instance (optional) |
|
23 | // init_state_callback : callback (optional) | |
|
24 | // Called once when the first state message is recieved from | |
|
25 | // the back-end. | |
|
23 | 26 | this.widget_manager = widget_manager; |
|
27 | this.init_state_callback = init_state_callback; | |
|
24 | 28 | this._buffered_state_diff = {}; |
|
25 | 29 | this.pending_msgs = 0; |
|
26 | 30 | this.msg_buffer = null; |
@@ -70,6 +74,10 define(["widgets/js/manager", | |||
|
70 | 74 | switch (method) { |
|
71 | 75 | case 'update': |
|
72 | 76 | this.set_state(msg.content.data.state); |
|
77 | if (this.init_state_callback) { | |
|
78 | this.init_state_callback.apply(this, [this]); | |
|
79 | this.init_state_callback = null; | |
|
80 | } | |
|
73 | 81 | break; |
|
74 | 82 | case 'custom': |
|
75 | 83 | this.trigger('msg:custom', msg.content.data.content); |
@@ -142,7 +142,7 class Widget(LoggingConfigurable): | |||
|
142 | 142 | |
|
143 | 143 | Widget._call_widget_constructed(self) |
|
144 | 144 | if open_comm: |
|
145 |
|
|
|
145 | self.open() | |
|
146 | 146 | |
|
147 | 147 | def __del__(self): |
|
148 | 148 | """Object disposal""" |
@@ -161,17 +161,18 class Widget(LoggingConfigurable): | |||
|
161 | 161 | if self._model_id is not None: |
|
162 | 162 | args['comm_id'] = self._model_id |
|
163 | 163 | self.set_comm(Comm(**args)) |
|
164 | ||
|
165 | # first update | |
|
166 | self.send_state() | |
|
167 | 164 | |
|
168 | 165 | def set_comm(self, comm): |
|
169 | 166 | """Set's the comm of the widget.""" |
|
170 | 167 | self.comm = comm |
|
171 | self._model_id = self.model_id | |
|
168 | self._model_id = self.model_id | |
|
169 | ||
|
170 | self.comm.on_msg(self._handle_msg) | |
|
171 | Widget.widgets[self.model_id] = self | |
|
172 | 172 | |
|
173 | self.comm.on_msg(self._handle_msg) | |
|
174 | Widget.widgets[self.model_id] = self | |
|
173 | # first update | |
|
174 | self.send_state() | |
|
175 | ||
|
175 | 176 | |
|
176 | 177 | @property |
|
177 | 178 | def model_id(self): |
General Comments 0
You need to be logged in to leave comments.
Login now