##// END OF EJS Templates
Re-decoupled comm_id from widget models
Jonathan Frederic -
Show More
@@ -66,60 +66,62 b''
66 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
66 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
67 this.widget_view_types[widget_view_name] = widget_view_type;
67 this.widget_view_types[widget_view_name] = widget_view_type;
68 };
68 };
69 WidgetManager.prototype.handle_msg = function(msg, model) {
69
70 var method = msg.content.data.method;
70
71 switch (method) {
71 WidgetManager.prototype.handle_msg = function(msg, model) {
72 case 'display':
72 var method = msg.content.data.method;
73 var cell = this.get_msg_cell(msg.parent_header.msg_id);
73 switch (method) {
74 if (cell === null) {
74 case 'display':
75 console.log("Could not determine where the display" +
75 var cell = this.get_msg_cell(msg.parent_header.msg_id);
76 " message was from. Widget will not be displayed");
76 if (cell === null) {
77 } else {
77 console.log("Could not determine where the display" +
78 var view = this.create_view(model,
78 " message was from. Widget will not be displayed");
79 msg.content.data.view_name, cell);
79 } else {
80 if (view !== undefined
80 var view = this.create_view(model,
81 && cell.widget_subarea !== undefined
81 msg.content.data.view_name, cell);
82 && cell.widget_subarea !== null) {
82 if (view !== undefined
83 cell.widget_area.show();
83 && cell.widget_subarea !== undefined
84 cell.widget_subarea.append(view.$el);
84 && cell.widget_subarea !== null) {
85 }
85 cell.widget_area.show();
86 cell.widget_subarea.append(view.$el);
87 }
88 }
89 break;
86 }
90 }
87 break;
88 }
91 }
89 }
90
92
91 WidgetManager.prototype.create_view = function(model, view_name, cell) {
93 WidgetManager.prototype.create_view = function(model, view_name, cell) {
92 view_name = view_name || model.get('default_view_name');
94 view_name = view_name || model.get('default_view_name');
93 var ViewType = this.widget_view_types[view_name];
95 var ViewType = this.widget_view_types[view_name];
94 if (ViewType !== undefined && ViewType !== null) {
96 if (ViewType !== undefined && ViewType !== null) {
95 var view = new ViewType({model: model, widget_manager: this, cell: cell});
97 var view = new ViewType({model: model, widget_manager: this, cell: cell});
96 view.render();
98 view.render();
97 model.views.push(view);
99 model.views.push(view);
98 model.on('destroy', view.remove, view);
100 model.on('destroy', view.remove, view);
99 /*
101 /*
100 // TODO: handle view deletion. Don't forget to delete child views
102 // TODO: handle view deletion. Don't forget to delete child views
101 var that = this;
103 var that = this;
102 view.$el.on("remove", function () {
104 view.$el.on("remove", function () {
103 var index = that.views.indexOf(view);
105 var index = that.views.indexOf(view);
104 if (index > -1) {
106 if (index > -1) {
105 that.views.splice(index, 1);
107 that.views.splice(index, 1);
106 }
107 view.remove(); // Clean-up view
108
109 // Close the comm if there are no views left.
110 if (that.views.length() === 0) {
111 //trigger comm close event?
112 }
113
114
115 if (that.comm !== undefined) {
116 that.comm.close();
117 delete that.comm.model; // Delete ref so GC will collect widget model.
118 delete that.comm;
119 }
108 }
120 delete that.widget_id; // Delete id from model so widget manager cleans up.
109 view.remove(); // Clean-up view
121 });
110
122 */
111 // Close the comm if there are no views left.
112 if (that.views.length() === 0) {
113 //trigger comm close event?
114 }
115
116
117 if (that.comm !== undefined) {
118 that.comm.close();
119 delete that.comm.model; // Delete ref so GC will collect widget model.
120 delete that.comm;
121 }
122 delete that.model_id; // Delete id from model so widget manager cleans up.
123 });
124 */
123 return view;
125 return view;
124 }
126 }
125 },
127 },
@@ -154,9 +156,9 b''
154 };
156 };
155
157
156
158
157 WidgetManager.prototype.get_model = function (widget_id) {
159 WidgetManager.prototype.get_model = function (model_id) {
158 var model = this._model_instances[widget_id];
160 var model = this._model_instances[model_id];
159 if (model !== undefined && model.id == widget_id) {
161 if (model !== undefined && model.id == model_id) {
160 return model;
162 return model;
161 }
163 }
162 return null;
164 return null;
@@ -191,7 +193,7 b''
191 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
193 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
192 var widget_type_name = msg.content.target_name;
194 var widget_type_name = msg.content.target_name;
193 var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm);
195 var widget_model = new this.widget_model_types[widget_type_name](this, comm.comm_id, comm);
194 this._model_instances[comm.comm_id] = widget_model;
196 this._model_instances[comm.comm_id] = widget_model; // comm_id == model_id
195 this._handle_create_widget(widget_model);
197 this._handle_create_widget(widget_model);
196 };
198 };
197
199
@@ -23,12 +23,12 b' function(widget_manager, underscore, backbone){'
23 // WidgetModel class
23 // WidgetModel class
24 //--------------------------------------------------------------------
24 //--------------------------------------------------------------------
25 var WidgetModel = Backbone.Model.extend({
25 var WidgetModel = Backbone.Model.extend({
26 constructor: function (widget_manager, widget_id, comm) {
26 constructor: function (widget_manager, model_id, comm) {
27 this.widget_manager = widget_manager;
27 this.widget_manager = widget_manager;
28 this.pending_msgs = 0;
28 this.pending_msgs = 0;
29 this.msg_throttle = 3;
29 this.msg_throttle = 3;
30 this.msg_buffer = null;
30 this.msg_buffer = null;
31 this.id = widget_id;
31 this.id = model_id;
32 this.views = [];
32 this.views = [];
33
33
34 if (comm !== undefined) {
34 if (comm !== undefined) {
@@ -55,7 +55,7 b' function(widget_manager, underscore, backbone){'
55 this.trigger('comm:close');
55 this.trigger('comm:close');
56 delete this.comm.model; // Delete ref so GC will collect widget model.
56 delete this.comm.model; // Delete ref so GC will collect widget model.
57 delete this.comm;
57 delete this.comm;
58 delete this.widget_id; // Delete id from model so widget manager cleans up.
58 delete this.model_id; // Delete id from model so widget manager cleans up.
59 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
59 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
60 },
60 },
61
61
@@ -181,12 +181,12 b' function(widget_manager, underscore, backbone){'
181 // triggered on model change
181 // triggered on model change
182 },
182 },
183
183
184 child_view: function(comm_id, view_name) {
184 child_view: function(model_id, view_name) {
185 // create and return a child view, given a comm id for a model and (optionally) a view name
185 // create and return a child view, given a comm id for a model and (optionally) a view name
186 // if the view name is not given, it defaults to the model's default view attribute
186 // if the view name is not given, it defaults to the model's default view attribute
187 var child_model = this.comm_manager.comms[comm_id].model;
187 var child_model = this.widget_manager.get_model(model_id);
188 var child_view = this.widget_manager.create_view(child_model, view_name, this.cell);
188 var child_view = this.widget_manager.create_view(child_model, view_name, this.cell);
189 this.child_views[comm_id] = child_view;
189 this.child_views[model_id] = child_view;
190 return child_view;
190 return child_view;
191 },
191 },
192
192
@@ -99,6 +99,10 b' class BaseWidget(LoggingConfigurable):'
99 if self._comm is None:
99 if self._comm is None:
100 self._open_communication()
100 self._open_communication()
101 return self._comm
101 return self._comm
102
103 @property
104 def model_id(self):
105 return self._comm.comm_id
102
106
103 # Event handlers
107 # Event handlers
104 def _handle_msg(self, msg):
108 def _handle_msg(self, msg):
@@ -220,10 +224,10 b' class BaseWidget(LoggingConfigurable):'
220 # encoder to look for a _repr_json property before giving
224 # encoder to look for a _repr_json property before giving
221 # up encoding
225 # up encoding
222 if isinstance(value, BaseWidget):
226 if isinstance(value, BaseWidget):
223 value = value.comm.comm_id
227 value = value.model_id
224 elif isinstance(value, list) and len(value)>0 and isinstance(value[0], BaseWidget):
228 elif isinstance(value, list) and len(value)>0 and isinstance(value[0], BaseWidget):
225 # assume all elements of the list are widgets
229 # assume all elements of the list are widgets
226 value = [i.comm.comm_id for i in value]
230 value = [i.model_id for i in value]
227 state[k] = value
231 state[k] = value
228 return state
232 return state
229
233
General Comments 0
You need to be logged in to leave comments. Login now