diff --git a/IPython/html/static/notebook/js/widgetmanager.js b/IPython/html/static/notebook/js/widgetmanager.js index 752fdf4..85fc44a 100644 --- a/IPython/html/static/notebook/js/widgetmanager.js +++ b/IPython/html/static/notebook/js/widgetmanager.js @@ -66,60 +66,62 @@ WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { this.widget_view_types[widget_view_name] = widget_view_type; }; - WidgetManager.prototype.handle_msg = function(msg, model) { - var method = msg.content.data.method; - switch (method) { - case 'display': - var cell = this.get_msg_cell(msg.parent_header.msg_id); - if (cell === null) { - console.log("Could not determine where the display" + - " message was from. Widget will not be displayed"); - } else { - var view = this.create_view(model, - msg.content.data.view_name, cell); - if (view !== undefined - && cell.widget_subarea !== undefined - && cell.widget_subarea !== null) { - cell.widget_area.show(); - cell.widget_subarea.append(view.$el); - } + + + WidgetManager.prototype.handle_msg = function(msg, model) { + var method = msg.content.data.method; + switch (method) { + case 'display': + var cell = this.get_msg_cell(msg.parent_header.msg_id); + if (cell === null) { + console.log("Could not determine where the display" + + " message was from. Widget will not be displayed"); + } else { + var view = this.create_view(model, + msg.content.data.view_name, cell); + if (view !== undefined + && cell.widget_subarea !== undefined + && cell.widget_subarea !== null) { + cell.widget_area.show(); + cell.widget_subarea.append(view.$el); + } + } + break; } - break; } - } - WidgetManager.prototype.create_view = function(model, view_name, cell) { - view_name = view_name || model.get('default_view_name'); + WidgetManager.prototype.create_view = function(model, view_name, cell) { + view_name = view_name || model.get('default_view_name'); var ViewType = this.widget_view_types[view_name]; if (ViewType !== undefined && ViewType !== null) { var view = new ViewType({model: model, widget_manager: this, cell: cell}); view.render(); - model.views.push(view); - model.on('destroy', view.remove, view); - /* - // TODO: handle view deletion. Don't forget to delete child views - var that = this; - view.$el.on("remove", function () { - var index = that.views.indexOf(view); - if (index > -1) { - that.views.splice(index, 1); - } - view.remove(); // Clean-up view - - // Close the comm if there are no views left. - if (that.views.length() === 0) { - //trigger comm close event? - } - - - if (that.comm !== undefined) { - that.comm.close(); - delete that.comm.model; // Delete ref so GC will collect widget model. - delete that.comm; + model.views.push(view); + model.on('destroy', view.remove, view); + /* + // TODO: handle view deletion. Don't forget to delete child views + var that = this; + view.$el.on("remove", function () { + var index = that.views.indexOf(view); + if (index > -1) { + that.views.splice(index, 1); } - delete that.widget_id; // Delete id from model so widget manager cleans up. - }); - */ + view.remove(); // Clean-up view + + // Close the comm if there are no views left. + if (that.views.length() === 0) { + //trigger comm close event? + } + + + if (that.comm !== undefined) { + that.comm.close(); + delete that.comm.model; // Delete ref so GC will collect widget model. + delete that.comm; + } + delete that.model_id; // Delete id from model so widget manager cleans up. + }); + */ return view; } }, @@ -154,9 +156,9 @@ }; - WidgetManager.prototype.get_model = function (widget_id) { - var model = this._model_instances[widget_id]; - if (model !== undefined && model.id == widget_id) { + WidgetManager.prototype.get_model = function (model_id) { + var model = this._model_instances[model_id]; + if (model !== undefined && model.id == model_id) { return model; } return null; @@ -191,7 +193,7 @@ WidgetManager.prototype._handle_comm_open = function (comm, msg) { var widget_type_name = msg.content.target_name; var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm); - this._model_instances[comm.comm_id] = widget_model; + this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id this._handle_create_widget(widget_model); }; diff --git a/IPython/html/static/notebook/js/widgets/base.js b/IPython/html/static/notebook/js/widgets/base.js index 2614e1c..9e013e4 100644 --- a/IPython/html/static/notebook/js/widgets/base.js +++ b/IPython/html/static/notebook/js/widgets/base.js @@ -23,12 +23,12 @@ function(widget_manager, underscore, backbone){ // WidgetModel class //-------------------------------------------------------------------- var WidgetModel = Backbone.Model.extend({ - constructor: function (widget_manager, widget_id, comm) { + constructor: function (widget_manager, model_id, comm) { this.widget_manager = widget_manager; this.pending_msgs = 0; this.msg_throttle = 3; this.msg_buffer = null; - this.id = widget_id; + this.id = model_id; this.views = []; if (comm !== undefined) { @@ -55,7 +55,7 @@ function(widget_manager, underscore, backbone){ this.trigger('comm:close'); delete this.comm.model; // Delete ref so GC will collect widget model. delete this.comm; - delete this.widget_id; // Delete id from model so widget manager cleans up. + delete this.model_id; // Delete id from model so widget manager cleans up. // TODO: Handle deletion, like this.destroy(), and delete views, etc. }, @@ -181,12 +181,12 @@ function(widget_manager, underscore, backbone){ // triggered on model change }, - child_view: function(comm_id, view_name) { + child_view: function(model_id, view_name) { // create and return a child view, given a comm id for a model and (optionally) a view name // if the view name is not given, it defaults to the model's default view attribute - var child_model = this.comm_manager.comms[comm_id].model; + var child_model = this.widget_manager.get_model(model_id); var child_view = this.widget_manager.create_view(child_model, view_name, this.cell); - this.child_views[comm_id] = child_view; + this.child_views[model_id] = child_view; return child_view; }, diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index fd8849e..7923d5f 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -99,6 +99,10 @@ class BaseWidget(LoggingConfigurable): if self._comm is None: self._open_communication() return self._comm + + @property + def model_id(self): + return self._comm.comm_id # Event handlers def _handle_msg(self, msg): @@ -220,10 +224,10 @@ class BaseWidget(LoggingConfigurable): # encoder to look for a _repr_json property before giving # up encoding if isinstance(value, BaseWidget): - value = value.comm.comm_id + value = value.model_id elif isinstance(value, list) and len(value)>0 and isinstance(value[0], BaseWidget): # assume all elements of the list are widgets - value = [i.comm.comm_id for i in value] + value = [i.model_id for i in value] state[k] = value return state