##// END OF EJS Templates
Destroy backbone model on comm:close
Sylvain Corlay -
Show More
@@ -1,486 +1,487 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define(["widgets/js/manager",
5 5 "underscore",
6 6 "backbone",
7 7 "jquery",
8 8 "base/js/namespace",
9 9 ], function(widgetmanager, _, Backbone, $, IPython){
10 10
11 11 var WidgetModel = Backbone.Model.extend({
12 12 constructor: function (widget_manager, model_id, comm) {
13 13 // Constructor
14 14 //
15 15 // Creates a WidgetModel instance.
16 16 //
17 17 // Parameters
18 18 // ----------
19 19 // widget_manager : WidgetManager instance
20 20 // model_id : string
21 21 // An ID unique to this model.
22 22 // comm : Comm instance (optional)
23 23 this.widget_manager = widget_manager;
24 24 this._buffered_state_diff = {};
25 25 this.pending_msgs = 0;
26 26 this.msg_buffer = null;
27 27 this.key_value_lock = null;
28 28 this.id = model_id;
29 29 this.views = [];
30 30
31 31 if (comm !== undefined) {
32 32 // Remember comm associated with the model.
33 33 this.comm = comm;
34 34 comm.model = this;
35 35
36 36 // Hook comm messages up to model.
37 37 comm.on_close($.proxy(this._handle_comm_closed, this));
38 38 comm.on_msg($.proxy(this._handle_comm_msg, this));
39 39 }
40 40 return Backbone.Model.apply(this);
41 41 },
42 42
43 43 send: function (content, callbacks) {
44 44 // Send a custom msg over the comm.
45 45 if (this.comm !== undefined) {
46 46 var data = {method: 'custom', content: content};
47 47 this.comm.send(data, callbacks);
48 48 this.pending_msgs++;
49 49 }
50 50 },
51 51
52 52 _handle_comm_closed: function (msg) {
53 53 // Handle when a widget is closed.
54 54 this.trigger('comm:close');
55 this.comm.model.trigger('destroy', this.comm.model);
55 56 delete this.comm.model; // Delete ref so GC will collect widget model.
56 57 delete this.comm;
57 58 delete this.model_id; // Delete id from model so widget manager cleans up.
58 59 _.each(this.views, function(view, i) {
59 60 view.remove();
60 61 });
61 62 },
62 63
63 64 _handle_comm_msg: function (msg) {
64 65 // Handle incoming comm msg.
65 66 var method = msg.content.data.method;
66 67 switch (method) {
67 68 case 'update':
68 69 this.apply_update(msg.content.data.state);
69 70 break;
70 71 case 'custom':
71 72 this.trigger('msg:custom', msg.content.data.content);
72 73 break;
73 74 case 'display':
74 75 this.widget_manager.display_view(msg, this);
75 76 break;
76 77 }
77 78 },
78 79
79 80 apply_update: function (state) {
80 81 // Handle when a widget is updated via the python side.
81 82 var that = this;
82 83 _.each(state, function(value, key) {
83 84 that.key_value_lock = [key, value];
84 85 try {
85 86 WidgetModel.__super__.set.apply(that, [key, that._unpack_models(value)]);
86 87 } finally {
87 88 that.key_value_lock = null;
88 89 }
89 90 });
90 91 },
91 92
92 93 _handle_status: function (msg, callbacks) {
93 94 // Handle status msgs.
94 95
95 96 // execution_state : ('busy', 'idle', 'starting')
96 97 if (this.comm !== undefined) {
97 98 if (msg.content.execution_state ==='idle') {
98 99 // Send buffer if this message caused another message to be
99 100 // throttled.
100 101 if (this.msg_buffer !== null &&
101 102 (this.get('msg_throttle') || 3) === this.pending_msgs) {
102 103 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
103 104 this.comm.send(data, callbacks);
104 105 this.msg_buffer = null;
105 106 } else {
106 107 --this.pending_msgs;
107 108 }
108 109 }
109 110 }
110 111 },
111 112
112 113 callbacks: function(view) {
113 114 // Create msg callbacks for a comm msg.
114 115 var callbacks = this.widget_manager.callbacks(view);
115 116
116 117 if (callbacks.iopub === undefined) {
117 118 callbacks.iopub = {};
118 119 }
119 120
120 121 var that = this;
121 122 callbacks.iopub.status = function (msg) {
122 123 that._handle_status(msg, callbacks);
123 124 };
124 125 return callbacks;
125 126 },
126 127
127 128 set: function(key, val, options) {
128 129 // Set a value.
129 130 var return_value = WidgetModel.__super__.set.apply(this, arguments);
130 131
131 132 // Backbone only remembers the diff of the most recent set()
132 133 // operation. Calling set multiple times in a row results in a
133 134 // loss of diff information. Here we keep our own running diff.
134 135 this._buffered_state_diff = $.extend(this._buffered_state_diff, this.changedAttributes() || {});
135 136 return return_value;
136 137 },
137 138
138 139 sync: function (method, model, options) {
139 140 // Handle sync to the back-end. Called when a model.save() is called.
140 141
141 142 // Make sure a comm exists.
142 143 var error = options.error || function() {
143 144 console.error('Backbone sync error:', arguments);
144 145 };
145 146 if (this.comm === undefined) {
146 147 error();
147 148 return false;
148 149 }
149 150
150 151 // Delete any key value pairs that the back-end already knows about.
151 152 var attrs = (method === 'patch') ? options.attrs : model.toJSON(options);
152 153 if (this.key_value_lock !== null) {
153 154 var key = this.key_value_lock[0];
154 155 var value = this.key_value_lock[1];
155 156 if (attrs[key] === value) {
156 157 delete attrs[key];
157 158 }
158 159 }
159 160
160 161 // Only sync if there are attributes to send to the back-end.
161 162 attrs = this._pack_models(attrs);
162 163 if (_.size(attrs) > 0) {
163 164
164 165 // If this message was sent via backbone itself, it will not
165 166 // have any callbacks. It's important that we create callbacks
166 167 // so we can listen for status messages, etc...
167 168 var callbacks = options.callbacks || this.callbacks();
168 169
169 170 // Check throttle.
170 171 if (this.pending_msgs >= (this.get('msg_throttle') || 3)) {
171 172 // The throttle has been exceeded, buffer the current msg so
172 173 // it can be sent once the kernel has finished processing
173 174 // some of the existing messages.
174 175
175 176 // Combine updates if it is a 'patch' sync, otherwise replace updates
176 177 switch (method) {
177 178 case 'patch':
178 179 this.msg_buffer = $.extend(this.msg_buffer || {}, attrs);
179 180 break;
180 181 case 'update':
181 182 case 'create':
182 183 this.msg_buffer = attrs;
183 184 break;
184 185 default:
185 186 error();
186 187 return false;
187 188 }
188 189 this.msg_buffer_callbacks = callbacks;
189 190
190 191 } else {
191 192 // We haven't exceeded the throttle, send the message like
192 193 // normal.
193 194 var data = {method: 'backbone', sync_data: attrs};
194 195 this.comm.send(data, callbacks);
195 196 this.pending_msgs++;
196 197 }
197 198 }
198 199 // Since the comm is a one-way communication, assume the message
199 200 // arrived. Don't call success since we don't have a model back from the server
200 201 // this means we miss out on the 'sync' event.
201 202 this._buffered_state_diff = {};
202 203 },
203 204
204 205 save_changes: function(callbacks) {
205 206 // Push this model's state to the back-end
206 207 //
207 208 // This invokes a Backbone.Sync.
208 209 this.save(this._buffered_state_diff, {patch: true, callbacks: callbacks});
209 210 },
210 211
211 212 _pack_models: function(value) {
212 213 // Replace models with model ids recursively.
213 214 var that = this;
214 215 var packed;
215 216 if (value instanceof Backbone.Model) {
216 217 return "IPY_MODEL_" + value.id;
217 218
218 219 } else if ($.isArray(value)) {
219 220 packed = [];
220 221 _.each(value, function(sub_value, key) {
221 222 packed.push(that._pack_models(sub_value));
222 223 });
223 224 return packed;
224 225
225 226 } else if (value instanceof Object) {
226 227 packed = {};
227 228 _.each(value, function(sub_value, key) {
228 229 packed[key] = that._pack_models(sub_value);
229 230 });
230 231 return packed;
231 232
232 233 } else {
233 234 return value;
234 235 }
235 236 },
236 237
237 238 _unpack_models: function(value) {
238 239 // Replace model ids with models recursively.
239 240 var that = this;
240 241 var unpacked;
241 242 if ($.isArray(value)) {
242 243 unpacked = [];
243 244 _.each(value, function(sub_value, key) {
244 245 unpacked.push(that._unpack_models(sub_value));
245 246 });
246 247 return unpacked;
247 248
248 249 } else if (value instanceof Object) {
249 250 unpacked = {};
250 251 _.each(value, function(sub_value, key) {
251 252 unpacked[key] = that._unpack_models(sub_value);
252 253 });
253 254 return unpacked;
254 255
255 256 } else if (typeof value === 'string' && value.slice(0,10) === "IPY_MODEL_") {
256 257 var model = this.widget_manager.get_model(value.slice(10, value.length));
257 258 if (model) {
258 259 return model;
259 260 } else {
260 261 return value;
261 262 }
262 263 } else {
263 264 return value;
264 265 }
265 266 },
266 267
267 268 });
268 269 widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel);
269 270
270 271
271 272 var WidgetView = Backbone.View.extend({
272 273 initialize: function(parameters) {
273 274 // Public constructor.
274 275 this.model.on('change',this.update,this);
275 276 this.options = parameters.options;
276 277 this.child_model_views = {};
277 278 this.child_views = {};
278 279 this.model.views.push(this);
279 280 this.id = this.id || IPython.utils.uuid();
280 281 this.on('displayed', function() {
281 282 this.is_displayed = true;
282 283 }, this);
283 284 },
284 285
285 286 update: function(){
286 287 // Triggered on model change.
287 288 //
288 289 // Update view to be consistent with this.model
289 290 },
290 291
291 292 create_child_view: function(child_model, options) {
292 293 // Create and return a child view.
293 294 //
294 295 // -given a model and (optionally) a view name if the view name is
295 296 // not given, it defaults to the model's default view attribute.
296 297
297 298 // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior
298 299 // it would be great to have the widget manager add the cell metadata
299 300 // to the subview without having to add it here.
300 301 options = $.extend({ parent: this }, options || {});
301 302 var child_view = this.model.widget_manager.create_view(child_model, options, this);
302 303
303 304 // Associate the view id with the model id.
304 305 if (this.child_model_views[child_model.id] === undefined) {
305 306 this.child_model_views[child_model.id] = [];
306 307 }
307 308 this.child_model_views[child_model.id].push(child_view.id);
308 309
309 310 // Remember the view by id.
310 311 this.child_views[child_view.id] = child_view;
311 312 return child_view;
312 313 },
313 314
314 315 pop_child_view: function(child_model) {
315 316 // Delete a child view that was previously created using create_child_view.
316 317 var view_ids = this.child_model_views[child_model.id];
317 318 if (view_ids !== undefined) {
318 319
319 320 // Only delete the first view in the list.
320 321 var view_id = view_ids[0];
321 322 var view = this.child_views[view_id];
322 323 delete this.child_views[view_id];
323 324 view_ids.splice(0,1);
324 325 child_model.views.pop(view);
325 326
326 327 // Remove the view list specific to this model if it is empty.
327 328 if (view_ids.length === 0) {
328 329 delete this.child_model_views[child_model.id];
329 330 }
330 331 return view;
331 332 }
332 333 return null;
333 334 },
334 335
335 336 do_diff: function(old_list, new_list, removed_callback, added_callback) {
336 337 // Difference a changed list and call remove and add callbacks for
337 338 // each removed and added item in the new list.
338 339 //
339 340 // Parameters
340 341 // ----------
341 342 // old_list : array
342 343 // new_list : array
343 344 // removed_callback : Callback(item)
344 345 // Callback that is called for each item removed.
345 346 // added_callback : Callback(item)
346 347 // Callback that is called for each item added.
347 348
348 349 // Walk the lists until an unequal entry is found.
349 350 var i;
350 351 for (i = 0; i < new_list.length; i++) {
351 352 if (i >= old_list.length || new_list[i] !== old_list[i]) {
352 353 break;
353 354 }
354 355 }
355 356
356 357 // Remove the non-matching items from the old list.
357 358 for (var j = i; j < old_list.length; j++) {
358 359 removed_callback(old_list[j]);
359 360 }
360 361
361 362 // Add the rest of the new list items.
362 363 for (; i < new_list.length; i++) {
363 364 added_callback(new_list[i]);
364 365 }
365 366 },
366 367
367 368 callbacks: function(){
368 369 // Create msg callbacks for a comm msg.
369 370 return this.model.callbacks(this);
370 371 },
371 372
372 373 render: function(){
373 374 // Render the view.
374 375 //
375 376 // By default, this is only called the first time the view is created
376 377 },
377 378
378 379 show: function(){
379 380 // Show the widget-area
380 381 if (this.options && this.options.cell &&
381 382 this.options.cell.widget_area !== undefined) {
382 383 this.options.cell.widget_area.show();
383 384 }
384 385 },
385 386
386 387 send: function (content) {
387 388 // Send a custom msg associated with this view.
388 389 this.model.send(content, this.callbacks());
389 390 },
390 391
391 392 touch: function () {
392 393 this.model.save_changes(this.callbacks());
393 394 },
394 395
395 396 after_displayed: function (callback, context) {
396 397 // Calls the callback right away is the view is already displayed
397 398 // otherwise, register the callback to the 'displayed' event.
398 399 if (this.is_displayed) {
399 400 callback.apply(context);
400 401 } else {
401 402 this.on('displayed', callback, context);
402 403 }
403 404 },
404 405 });
405 406
406 407
407 408 var DOMWidgetView = WidgetView.extend({
408 409 initialize: function (parameters) {
409 410 // Public constructor
410 411 DOMWidgetView.__super__.initialize.apply(this, [parameters]);
411 412 this.on('displayed', this.show, this);
412 413 this.after_displayed(function() {
413 414 this.update_visible(this.model, this.model.get("visible"));
414 415 this.update_css(this.model, this.model.get("_css"));
415 416 }, this);
416 417 this.model.on('msg:custom', this.on_msg, this);
417 418 this.model.on('change:visible', this.update_visible, this);
418 419 this.model.on('change:_css', this.update_css, this);
419 420 },
420 421
421 422 on_msg: function(msg) {
422 423 // Handle DOM specific msgs.
423 424 switch(msg.msg_type) {
424 425 case 'add_class':
425 426 this.add_class(msg.selector, msg.class_list);
426 427 break;
427 428 case 'remove_class':
428 429 this.remove_class(msg.selector, msg.class_list);
429 430 break;
430 431 }
431 432 },
432 433
433 434 add_class: function (selector, class_list) {
434 435 // Add a DOM class to an element.
435 436 this._get_selector_element(selector).addClass(class_list);
436 437 },
437 438
438 439 remove_class: function (selector, class_list) {
439 440 // Remove a DOM class from an element.
440 441 this._get_selector_element(selector).removeClass(class_list);
441 442 },
442 443
443 444 update_visible: function(model, value) {
444 445 // Update visibility
445 446 this.$el.toggle(value);
446 447 },
447 448
448 449 update_css: function (model, css) {
449 450 // Update the css styling of this view.
450 451 var e = this.$el;
451 452 if (css === undefined) {return;}
452 453 for (var i = 0; i < css.length; i++) {
453 454 // Apply the css traits to all elements that match the selector.
454 455 var selector = css[i][0];
455 456 var elements = this._get_selector_element(selector);
456 457 if (elements.length > 0) {
457 458 var trait_key = css[i][1];
458 459 var trait_value = css[i][2];
459 460 elements.css(trait_key ,trait_value);
460 461 }
461 462 }
462 463 },
463 464
464 465 _get_selector_element: function (selector) {
465 466 // Get the elements via the css selector.
466 467 var elements;
467 468 if (!selector) {
468 469 elements = this.$el;
469 470 } else {
470 471 elements = this.$el.find(selector).addBack(selector);
471 472 }
472 473 return elements;
473 474 },
474 475 });
475 476
476 477 var widget = {
477 478 'WidgetModel': WidgetModel,
478 479 'WidgetView': WidgetView,
479 480 'DOMWidgetView': DOMWidgetView,
480 481 };
481 482
482 483 // For backwards compatability.
483 484 $.extend(IPython, widget);
484 485
485 486 return widget;
486 487 });
General Comments 0
You need to be logged in to leave comments. Login now