Show More
@@ -339,7 +339,7 b' function(WidgetManager, _, Backbone){' | |||
|
339 | 339 | return null; |
|
340 | 340 | }, |
|
341 | 341 | |
|
342 |
do_diff: function(old_list, new_list, removed_callback, added_callback |
|
|
342 | do_diff: function(old_list, new_list, removed_callback, added_callback) { | |
|
343 | 343 | // Difference a changed list and call remove and add callbacks for |
|
344 | 344 | // each removed and added item in the new list. |
|
345 | 345 | // |
@@ -351,60 +351,24 b' function(WidgetManager, _, Backbone){' | |||
|
351 | 351 | // Callback that is called for each item removed. |
|
352 | 352 | // added_callback : Callback(item) |
|
353 | 353 | // Callback that is called for each item added. |
|
354 | // [respect_order] : bool [True] | |
|
355 | // Whether or not the order of the list matters. | |
|
356 | ||
|
357 | if (respect_order || respect_order===undefined) { | |
|
358 | // Walk the lists until an unequal entry is found. | |
|
359 | var i; | |
|
360 | for (i = 0; i < new_list.length; i++) { | |
|
361 | if (i < old_list.length || new_list[i] !== old_list[i]) { | |
|
362 | break; | |
|
363 | } | |
|
364 | } | |
|
365 | 354 | |
|
366 | // Remove the non-matching items from the old list. | |
|
367 | for (var j = i; j < old_list.length; j++) { | |
|
368 | removed_callback(old_list[j]); | |
|
355 | // Walk the lists until an unequal entry is found. | |
|
356 | var i; | |
|
357 | for (i = 0; i < new_list.length; i++) { | |
|
358 | if (i < old_list.length || new_list[i] !== old_list[i]) { | |
|
359 | break; | |
|
369 | 360 | } |
|
361 | } | |
|
370 | 362 | |
|
371 | // Add the rest of the new list items. | |
|
372 |
|
|
|
373 |
|
|
|
374 | } | |
|
375 | } else { | |
|
376 | // removed items | |
|
377 | _.each(this.difference(old_list, new_list), function(item, index, list) { | |
|
378 | removed_callback(item); | |
|
379 | }, this); | |
|
380 | ||
|
381 | // added items | |
|
382 | _.each(this.difference(new_list, old_list), function(item, index, list) { | |
|
383 | added_callback(item); | |
|
384 | }, this); | |
|
363 | // Remove the non-matching items from the old list. | |
|
364 | for (var j = i; j < old_list.length; j++) { | |
|
365 | removed_callback(old_list[j]); | |
|
385 | 366 | } |
|
386 | }, | |
|
387 | 367 | |
|
388 | difference: function(a, b) { | |
|
389 | // Calculate the difference of two lists by contents. | |
|
390 | // | |
|
391 | // This function is like the underscore difference function | |
|
392 | // except it will not fail when given a list with duplicates. | |
|
393 | // i.e.: | |
|
394 | // diff([1, 2, 2, 3], [3, 2]) | |
|
395 | // Underscores results: | |
|
396 | // [1] | |
|
397 | // This method: | |
|
398 | // [1, 2] | |
|
399 | var contents = a.slice(0); | |
|
400 | var found_index; | |
|
401 | for (var i = 0; i < b.length; i++) { | |
|
402 | found_index = _.indexOf(contents, b[i]); | |
|
403 | if (found_index >= 0) { | |
|
404 | contents.splice(found_index, 1); | |
|
405 | } | |
|
368 | // Add the rest of the new list items. | |
|
369 | for (i; i < new_list.length; i++) { | |
|
370 | added_callback(new_list[i]); | |
|
406 | 371 | } |
|
407 | return contents; | |
|
408 | 372 | }, |
|
409 | 373 | |
|
410 | 374 | callbacks: function(){ |
@@ -21,7 +21,6 b' define(["widgets/js/widget"], function(WidgetManager) {' | |||
|
21 | 21 | // Called when view is rendered. |
|
22 | 22 | this.$el.addClass('widget-container') |
|
23 | 23 | .addClass('vbox'); |
|
24 | this.children={}; | |
|
25 | 24 | this.update_children([], this.model.get('children')); |
|
26 | 25 | this.model.on('change:children', function(model, value, options) { |
|
27 | 26 | this.update_children(model.previous('children'), value); |
@@ -80,7 +79,6 b' define(["widgets/js/widget"], function(WidgetManager) {' | |||
|
80 | 79 | render: function(){ |
|
81 | 80 | // Called when view is rendered. |
|
82 | 81 | var that = this; |
|
83 | this.children={}; | |
|
84 | 82 | |
|
85 | 83 | this.$el.on("remove", function(){ |
|
86 | 84 | that.$backdrop.remove(); |
General Comments 0
You need to be logged in to leave comments.
Login now