##// END OF EJS Templates
s/long thing/set_state
Jonathan Frederic -
Show More
@@ -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._handle_receive_state(sync_data) # handles all methods
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