Show More
@@ -337,7 +337,7 function(WidgetManager, _, Backbone){ | |||||
337 | return null; |
|
337 | return null; | |
338 | }, |
|
338 | }, | |
339 |
|
339 | |||
340 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
340 | do_diff: function(old_list, new_list, removed_callback, added_callback, respect_order) { | |
341 | // Difference a changed list and call remove and add callbacks for |
|
341 | // Difference a changed list and call remove and add callbacks for | |
342 | // each removed and added item in the new list. |
|
342 | // each removed and added item in the new list. | |
343 | // |
|
343 | // | |
@@ -349,8 +349,29 function(WidgetManager, _, Backbone){ | |||||
349 | // Callback that is called for each item removed. |
|
349 | // Callback that is called for each item removed. | |
350 | // added_callback : Callback(item) |
|
350 | // added_callback : Callback(item) | |
351 | // Callback that is called for each item added. |
|
351 | // Callback that is called for each item added. | |
|
352 | // [respect_order] : bool [True] | |||
|
353 | // Whether or not the order of the list matters. | |||
|
354 | ||||
|
355 | if (respect_order || respect_order===undefined) { | |||
|
356 | // Walk the lists until an unequal entry is found. | |||
|
357 | var i; | |||
|
358 | for (i = 0; i < new_list.length; i++) { | |||
|
359 | if (i < old_list.length || new_list[i] !== old_list[i]) { | |||
|
360 | break; | |||
|
361 | } | |||
|
362 | } | |||
352 |
|
363 | |||
|
364 | // Remove the non-matching items from the old list. | |||
|
365 | for (var j = i; j < old_list.length; j++) { | |||
|
366 | console.log(j, old_list.length, old_list[j]); | |||
|
367 | removed_callback(old_list[j]); | |||
|
368 | } | |||
353 |
|
369 | |||
|
370 | // Add the rest of the new list items. | |||
|
371 | for (i; i < new_list.length; i++) { | |||
|
372 | added_callback(new_list[i]); | |||
|
373 | } | |||
|
374 | } else { | |||
354 | // removed items |
|
375 | // removed items | |
355 | _.each(this.difference(old_list, new_list), function(item, index, list) { |
|
376 | _.each(this.difference(old_list, new_list), function(item, index, list) { | |
356 | removed_callback(item); |
|
377 | removed_callback(item); | |
@@ -360,6 +381,7 function(WidgetManager, _, Backbone){ | |||||
360 | _.each(this.difference(new_list, old_list), function(item, index, list) { |
|
381 | _.each(this.difference(new_list, old_list), function(item, index, list) { | |
361 | added_callback(item); |
|
382 | added_callback(item); | |
362 | }, this); |
|
383 | }, this); | |
|
384 | } | |||
363 | }, |
|
385 | }, | |
364 |
|
386 | |||
365 | difference: function(a, b) { |
|
387 | difference: function(a, b) { |
General Comments 0
You need to be logged in to leave comments.
Login now