From 2b5ebb9e8f2a04b8e44b0c3d5caeb02801fcf112 Mon Sep 17 00:00:00 2001 From: Jonathan Frederic Date: Wed, 8 Oct 2014 20:59:56 -0700 Subject: [PATCH] Make Python push initial state. Also added initial state push callback. --- IPython/html/static/widgets/js/manager.js | 17 +++++++++++++---- IPython/html/static/widgets/js/widget.js | 10 +++++++++- IPython/html/widgets/widget.py | 15 ++++++++------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/IPython/html/static/widgets/js/manager.js b/IPython/html/static/widgets/js/manager.js index 6a0200fb4..2c2de8be8 100644 --- a/IPython/html/static/widgets/js/manager.js +++ b/IPython/html/static/widgets/js/manager.js @@ -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; diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index d60b38489..fb3a4049e 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -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); diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index 65c2a3307..d840d5bac 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -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):