diff --git a/IPython/html/static/notebook/js/widgetmanager.js b/IPython/html/static/notebook/js/widgetmanager.js index e2dfe20..6b3dfa5 100644 --- a/IPython/html/static/notebook/js/widgetmanager.js +++ b/IPython/html/static/notebook/js/widgetmanager.js @@ -34,14 +34,12 @@ // Attach a comm manager to the this.comm_manager = comm_manager; + this._models = {}; /* Dictionary of model ids and model instances */ // Register already-registered widget model types with the comm manager. - for (var name in WidgetManager._model_types) { - if (WidgetManager._model_types.hasOwnProperty(name)) { - this.comm_manager.register_target(name, $.proxy(this._handle_comm_open, this)); - - } - } + _.each(WidgetManager._model_types, function(value, key) { + this.comm_manager.register_target(value, $.proxy(this._handle_comm_open, this)); + }); }; //-------------------------------------------------------------------- @@ -49,7 +47,6 @@ //-------------------------------------------------------------------- WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ - WidgetManager._models = {}; /* Dictionary of model ids and model instances */ WidgetManager._managers = []; /* List of widget managers */ WidgetManager.register_widget_model = function (model_name, model_type) { @@ -58,12 +55,11 @@ // Register the widget with the comm manager. Make sure to pass this object's context // in so `this` works in the call back. - for (var i = 0; i < WidgetManager._managers.length; i++) { - var instance = WidgetManager._managers[i]; + _.each(WidgetManager._managers, function(instance, i) { if (instance.comm_manager !== null) { instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); } - } + }); }; WidgetManager.register_widget_view = function (view_name, view_type) { @@ -169,7 +165,7 @@ }; WidgetManager.prototype.get_model = function (model_id) { - var model = WidgetManager._models[model_id]; + var model = this._models[model_id]; if (model !== undefined && model.id == model_id) { return model; } @@ -180,7 +176,7 @@ var model_id = comm.comm_id; var widget_type_name = msg.content.target_name; var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); - WidgetManager._models[model_id] = widget_model; + this._models[model_id] = widget_model; }; IPython.WidgetManager = WidgetManager; diff --git a/IPython/html/static/notebook/js/widgets/widget.js b/IPython/html/static/notebook/js/widgets/widget.js index a6ac027..9a23283 100644 --- a/IPython/html/static/notebook/js/widgets/widget.js +++ b/IPython/html/static/notebook/js/widgets/widget.js @@ -86,17 +86,14 @@ function(WidgetManager, Underscore, Backbone){ apply_update: function (state) { // Handle when a widget is updated via the python side. - for (var key in state) { - if (state.hasOwnProperty(key)) { - var value = state[key]; - this.key_value_lock = [key, value]; - try { - this.set(key, this._unpack_models(value)); - } finally { - this.key_value_lock = null; - } + _.each(state, function(value, key) { + this.key_value_lock = [key, value]; + try { + this.set(key, this._unpack_models(value)); + } finally { + this.key_value_lock = null; } - } + }); }, _handle_status: function (msg, callbacks) { @@ -205,9 +202,9 @@ function(WidgetManager, Underscore, Backbone){ return value.id; } else if (value instanceof Object) { var packed = {}; - for (var key in value) { - packed[key] = this._pack_models(value[key]); - } + _.each(value, function(sub_value, key) { + packed[key] = this._pack_models(sub_value); + }); return packed; } else { return value; @@ -218,9 +215,9 @@ function(WidgetManager, Underscore, Backbone){ // Replace model ids with models recursively. if (value instanceof Object) { var unpacked = {}; - for (var key in value) { - unpacked[key] = this._unpack_models(value[key]); - } + _.each(value, function(sub_value, key) { + unpacked[key] = this._unpack_models(sub_value); + }); return unpacked; } else { var model = this.widget_manager.get_model(value); @@ -369,20 +366,16 @@ function(WidgetManager, Underscore, Backbone){ var css = this.model.get('_css'); if (css === undefined) {return;} - for (var selector in css) { - if (css.hasOwnProperty(selector)) { - // Apply the css traits to all elements that match the selector. - var elements = this._get_selector_element(selector); - if (elements.length > 0) { - var css_traits = css[selector]; - for (var css_key in css_traits) { - if (css_traits.hasOwnProperty(css_key)) { - elements.css(css_key, css_traits[css_key]); - } - } - } + _.each(css, function(css_traits, selector){ + // Apply the css traits to all elements that match the selector. + var elements = this._get_selector_element(selector); + if (elements.length > 0) { + _.each(css_traits, function(css_value, css_key){ + elements.css(css_key, css_value); + }); } - } + }); + }, _get_selector_element: function (selector) { diff --git a/IPython/html/static/notebook/js/widgets/widget_float_range.js b/IPython/html/static/notebook/js/widgets/widget_float_range.js index 37c1095..fd59ba2 100644 --- a/IPython/html/static/notebook/js/widgets/widget_float_range.js +++ b/IPython/html/static/notebook/js/widgets/widget_float_range.js @@ -50,12 +50,12 @@ define(["notebook/js/widgets/widget"], function(WidgetManager){ // JQuery slider option keys. These keys happen to have a // one-to-one mapping with the corrosponding keys of the model. var jquery_slider_keys = ['step', 'max', 'min', 'disabled']; - for (var index in jquery_slider_keys) { - var key = jquery_slider_keys[index]; - if (this.model.get(key) !== undefined) { - this.$slider.slider("option", key, this.model.get(key)); + _.each(jquery_slider_keys, function(key, i) { + var model_value = this.model.get(key); + if (model_value !== undefined) { + this.$slider.slider("option", key, model_value); } - } + }); // WORKAROUND FOR JQUERY SLIDER BUG. // The horizontal position of the slider handle diff --git a/IPython/html/static/notebook/js/widgets/widget_int_range.js b/IPython/html/static/notebook/js/widgets/widget_int_range.js index f01109b..7d26aec 100644 --- a/IPython/html/static/notebook/js/widgets/widget_int_range.js +++ b/IPython/html/static/notebook/js/widgets/widget_int_range.js @@ -49,12 +49,12 @@ define(["notebook/js/widgets/widget"], function(WidgetManager){ // JQuery slider option keys. These keys happen to have a // one-to-one mapping with the corrosponding keys of the model. var jquery_slider_keys = ['step', 'max', 'min', 'disabled']; - for (var index in jquery_slider_keys) { - var key = jquery_slider_keys[index]; - if (this.model.get(key) !== undefined) { - this.$slider.slider("option", key, this.model.get(key)); + _.each(jquery_slider_keys, function(key, i) { + var model_value = this.model.get(key); + if (model_value !== undefined) { + this.$slider.slider("option", key, model_value); } - } + }); // WORKAROUND FOR JQUERY SLIDER BUG. // The horizontal position of the slider handle diff --git a/IPython/html/static/notebook/js/widgets/widget_selection.js b/IPython/html/static/notebook/js/widgets/widget_selection.js index db35626..c95e34b 100644 --- a/IPython/html/static/notebook/js/widgets/widget_selection.js +++ b/IPython/html/static/notebook/js/widgets/widget_selection.js @@ -67,13 +67,13 @@ define(["notebook/js/widgets/widget"], function(WidgetManager){ var items = this.model.get('values'); var $replace_droplist = $('