Make Python push initial state.

Also added initial state push callback.
This commit is contained in:
Jonathan Frederic 2014-10-08 20:59:56 -07:00
parent e7ee9c12b3
commit 2b5ebb9e8f
3 changed files with 30 additions and 12 deletions

View File

@ -190,7 +190,7 @@ define([
return this.create_model({model_name: msg.content.data.target_name, comm: comm});
};
WidgetManager.prototype.create_model = function (model_name, target_name) {
WidgetManager.prototype.create_model = function (model_name, target_name, init_state_callback) {
// Create and return a new widget model.
//
// Parameters
@ -199,7 +199,13 @@ define([
// Target name of the widget model to create.
// target_name: string
// Target name of the widget in the back-end.
return this._create_model({model_name: model_name, target_name: target_name});
// init_state_callback: (optional) callback
// Called when the first state push from the back-end is
// recieved.
return this._create_model({
model_name: model_name,
target_name: target_name,
init_state_callback: init_state_callback});
};
WidgetManager.prototype._create_model = function (options) {
@ -214,6 +220,9 @@ define([
// target_name: (optional) string
// Target name of the widget in the back-end.
// comm: (optional) Comm
// init_state_callback: (optional) callback
// Called when the first state push from the back-end is
// recieved.
// Create a comm if it wasn't provided.
var comm = options.comm;
@ -226,8 +235,8 @@ define([
var instantiate_model = function(ModelType) {
var model_id = comm.comm_id;
var widget_model = new ModelType(that, model_id, comm);
widget_model.on('comm:close', function () {sss
var widget_model = new ModelType(that, model_id, comm, options.init_state_callback);
widget_model.on('comm:close', function () {
delete that._models[model_id];
});
that._models[model_id] = widget_model;

View File

@ -9,7 +9,7 @@ define(["widgets/js/manager",
], function(widgetmanager, _, Backbone, $, IPython){
var WidgetModel = Backbone.Model.extend({
constructor: function (widget_manager, model_id, comm) {
constructor: function (widget_manager, model_id, comm, init_state_callback) {
// Constructor
//
// Creates a WidgetModel instance.
@ -20,7 +20,11 @@ define(["widgets/js/manager",
// model_id : string
// An ID unique to this model.
// comm : Comm instance (optional)
// init_state_callback : callback (optional)
// Called once when the first state message is recieved from
// the back-end.
this.widget_manager = widget_manager;
this.init_state_callback = init_state_callback;
this._buffered_state_diff = {};
this.pending_msgs = 0;
this.msg_buffer = null;
@ -70,6 +74,10 @@ define(["widgets/js/manager",
switch (method) {
case 'update':
this.set_state(msg.content.data.state);
if (this.init_state_callback) {
this.init_state_callback.apply(this, [this]);
this.init_state_callback = null;
}
break;
case 'custom':
this.trigger('msg:custom', msg.content.data.content);

View File

@ -142,7 +142,7 @@ class Widget(LoggingConfigurable):
Widget._call_widget_constructed(self)
if open_comm:
self.open()
self.open()
def __del__(self):
"""Object disposal"""
@ -161,17 +161,18 @@ class Widget(LoggingConfigurable):
if self._model_id is not None:
args['comm_id'] = self._model_id
self.set_comm(Comm(**args))
# first update
self.send_state()
def set_comm(self, comm):
"""Set's the comm of the widget."""
self.comm = comm
self._model_id = self.model_id
self._model_id = self.model_id
self.comm.on_msg(self._handle_msg)
Widget.widgets[self.model_id] = self
self.comm.on_msg(self._handle_msg)
Widget.widgets[self.model_id] = self
# first update
self.send_state()
@property
def model_id(self):