From 92f4bb539236fbd4e522bd266cb8664149112eb6 2014-01-16 10:57:17 From: Jonathan Frederic Date: 2014-01-16 10:57:17 Subject: [PATCH] Add a helper method that acts on the changes made to a list. --- diff --git a/IPython/html/static/notebook/js/widgets/widget.js b/IPython/html/static/notebook/js/widgets/widget.js index 3c56647..5cbd811 100644 --- a/IPython/html/static/notebook/js/widgets/widget.js +++ b/IPython/html/static/notebook/js/widgets/widget.js @@ -224,6 +224,31 @@ function(widget_manager, underscore, backbone){ }, this); }, + do_diff: function(old_list, new_list, removed_callback, added_callback) { + // Difference a changed list and call remove and add callbacks for + // each removed and added item in the new list. + // + // Parameters + // ---------- + // old_list : array + // new_list : array + // removed_callback : Callback(item) + // Callback that is called for each item removed. + // added_callback : Callback(item) + // Callback that is called for each item added. + + + // removed items + _.each(_.difference(old_list, new_list), function(item, index, list) { + removed_callback(item); + }, this); + + // added items + _.each(_.difference(new_list, old_list), function(item, index, list) { + added_callback(item); + }, this); + } + callbacks: function(){ return this.widget_manager.callbacks(this); },