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