Show More
@@ -69,7 +69,7 b' define(["widgets/js/manager",' | |||||
69 | var method = msg.content.data.method; |
|
69 | var method = msg.content.data.method; | |
70 | switch (method) { |
|
70 | switch (method) { | |
71 | case 'update': |
|
71 | case 'update': | |
72 |
this. |
|
72 | this.set_state(msg.content.data.state); | |
73 | break; |
|
73 | break; | |
74 | case 'custom': |
|
74 | case 'custom': | |
75 | this.trigger('msg:custom', msg.content.data.content); |
|
75 | this.trigger('msg:custom', msg.content.data.content); | |
@@ -80,7 +80,7 b' define(["widgets/js/manager",' | |||||
80 | } |
|
80 | } | |
81 | }, |
|
81 | }, | |
82 |
|
82 | |||
83 |
|
|
83 | set_state: function (state) { | |
84 | // Handle when a widget is updated via the python side. |
|
84 | // Handle when a widget is updated via the python side. | |
85 | this.state_lock = state; |
|
85 | this.state_lock = state; | |
86 | try { |
|
86 | try { |
@@ -118,8 +118,8 b' casper.notebook_test(function () {' | |||||
118 | ' b = CInt(0, sync=True)\n' + |
|
118 | ' b = CInt(0, sync=True)\n' + | |
119 | ' c = CInt(0, sync=True)\n' + |
|
119 | ' c = CInt(0, sync=True)\n' + | |
120 | ' d = CInt(-1, sync=True)\n' + // See if it sends a full state. |
|
120 | ' d = CInt(-1, sync=True)\n' + // See if it sends a full state. | |
121 |
' def |
|
121 | ' def set_state(self, sync_data):\n' + | |
122 |
' widgets.Widget. |
|
122 | ' widgets.Widget.set_state(self, sync_data)\n'+ | |
123 | ' self.d = len(sync_data)\n' + |
|
123 | ' self.d = len(sync_data)\n' + | |
124 | 'multiset = MultiSetWidget()\n' + |
|
124 | 'multiset = MultiSetWidget()\n' + | |
125 | 'display(multiset)\n' + |
|
125 | 'display(multiset)\n' + |
@@ -208,6 +208,15 b' class Widget(LoggingConfigurable):' | |||||
208 | value = getattr(self, k) |
|
208 | value = getattr(self, k) | |
209 | state[k] = f(value) |
|
209 | state[k] = f(value) | |
210 | return state |
|
210 | return state | |
|
211 | ||||
|
212 | def set_state(self, sync_data): | |||
|
213 | """Called when a state is received from the front-end.""" | |||
|
214 | for name in self.keys: | |||
|
215 | if name in sync_data: | |||
|
216 | json_value = sync_data[name] | |||
|
217 | from_json = self.trait_metadata(name, 'from_json', self._trait_from_json) | |||
|
218 | with self._lock_property(name, json_value): | |||
|
219 | setattr(self, name, from_json(json_value)) | |||
211 |
|
220 | |||
212 | def send(self, content): |
|
221 | def send(self, content): | |
213 | """Sends a custom msg to the widget model in the front-end. |
|
222 | """Sends a custom msg to the widget model in the front-end. | |
@@ -304,22 +313,13 b' class Widget(LoggingConfigurable):' | |||||
304 | # Handle backbone sync methods CREATE, PATCH, and UPDATE all in one. |
|
313 | # Handle backbone sync methods CREATE, PATCH, and UPDATE all in one. | |
305 | if method == 'backbone' and 'sync_data' in data: |
|
314 | if method == 'backbone' and 'sync_data' in data: | |
306 | sync_data = data['sync_data'] |
|
315 | sync_data = data['sync_data'] | |
307 |
self. |
|
316 | self.set_state(sync_data) # handles all methods | |
308 |
|
317 | |||
309 | # Handle a custom msg from the front-end |
|
318 | # Handle a custom msg from the front-end | |
310 | elif method == 'custom': |
|
319 | elif method == 'custom': | |
311 | if 'content' in data: |
|
320 | if 'content' in data: | |
312 | self._handle_custom_msg(data['content']) |
|
321 | self._handle_custom_msg(data['content']) | |
313 |
|
322 | |||
314 | def _handle_receive_state(self, sync_data): |
|
|||
315 | """Called when a state is received from the front-end.""" |
|
|||
316 | for name in self.keys: |
|
|||
317 | if name in sync_data: |
|
|||
318 | json_value = sync_data[name] |
|
|||
319 | from_json = self.trait_metadata(name, 'from_json', self._trait_from_json) |
|
|||
320 | with self._lock_property(name, json_value): |
|
|||
321 | setattr(self, name, from_json(json_value)) |
|
|||
322 |
|
||||
323 | def _handle_custom_msg(self, content): |
|
323 | def _handle_custom_msg(self, content): | |
324 | """Called when a custom msg is received.""" |
|
324 | """Called when a custom msg is received.""" | |
325 | self._msg_callbacks(self, content) |
|
325 | self._msg_callbacks(self, content) |
General Comments 0
You need to be logged in to leave comments.
Login now