From 1eb2a30efb732d75028603d398924cc5b11d4e6f Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Fri, 18 Jul 2014 22:08:17 +0000 Subject: [PATCH 1/4] once-displayed --- IPython/html/static/widgets/js/widget.js | 13 +++++++ .../static/widgets/js/widget_container.js | 37 +++---------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index 29193e67f..d916b75ca 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -275,6 +275,9 @@ define(["widgets/js/manager", this.child_views = {}; this.model.views.push(this); this.id = this.id || IPython.utils.uuid(); + this.on('displayed', function() { + this.is_displayed = true; + }, this); }, update: function(){ @@ -386,6 +389,16 @@ define(["widgets/js/manager", touch: function () { this.model.save_changes(this.callbacks()); }, + + once_displayed: function (callback, context) { + // Calls the callback right away is the view is already displayed + // otherwise, register the callback to the 'displayed' event. + if (this.is_displayed) { + callback.apply(context); + } else { + this.on('displayed', callback, context); + } + }, }); diff --git a/IPython/html/static/widgets/js/widget_container.js b/IPython/html/static/widgets/js/widget_container.js index 427d3de04..211845e6f 100644 --- a/IPython/html/static/widgets/js/widget_container.js +++ b/IPython/html/static/widgets/js/widget_container.js @@ -17,18 +17,6 @@ define([ this.update_children(model.previous('children'), value); }, this); this.update(); - - // Trigger model displayed events for any models that are child to - // this model when this model is displayed. - var that = this; - this.on('displayed', function(){ - that.is_displayed = true; - for (var property in that.child_views) { - if (that.child_views.hasOwnProperty(property)) { - that.child_views[property].trigger('displayed'); - } - } - }); }, update_children: function(old_list, new_list) { @@ -49,10 +37,8 @@ define([ var view = this.create_child_view(model); this.$el.append(view.$el); - // Trigger the displayed event if this model is displayed. - if (this.is_displayed) { - view.trigger('displayed'); - } + // Trigger the displayed event once this model is displayed. + this.once_displayed(function() { view.trigger('displayed'); }, this); }, update: function(){ @@ -179,17 +165,6 @@ define([ this.update_children(model.previous('children'), value); }, this); this.update(); - - // Trigger model displayed events for any models that are child to - // this model when this model is displayed. - this.on('displayed', function(){ - that.is_displayed = true; - for (var property in that.child_views) { - if (that.child_views.hasOwnProperty(property)) { - that.child_views[property].trigger('displayed'); - } - } - }); }, hide: function() { @@ -252,10 +227,10 @@ define([ var view = this.create_child_view(model); this.$body.append(view.$el); - // Trigger the displayed event if this model is displayed. - if (this.is_displayed) { - view.trigger('displayed'); - } + // Trigger the displayed event once this model is displayed. + this.once_displayed(function() { + view.trigger('displayed'); + }, this); }, update: function(){ From 8bc4ea37a1a45c54e345b916df027d4149e1e172 Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Fri, 18 Jul 2014 22:26:21 +0000 Subject: [PATCH 2/4] unnecessary context variable specify --- IPython/html/static/widgets/js/widget_container.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/IPython/html/static/widgets/js/widget_container.js b/IPython/html/static/widgets/js/widget_container.js index 211845e6f..e545751f0 100644 --- a/IPython/html/static/widgets/js/widget_container.js +++ b/IPython/html/static/widgets/js/widget_container.js @@ -38,7 +38,9 @@ define([ this.$el.append(view.$el); // Trigger the displayed event once this model is displayed. - this.once_displayed(function() { view.trigger('displayed'); }, this); + this.once_displayed(function() { + view.trigger('displayed'); + }); }, update: function(){ @@ -228,9 +230,9 @@ define([ this.$body.append(view.$el); // Trigger the displayed event once this model is displayed. - this.once_displayed(function() { - view.trigger('displayed'); - }, this); + this.once_displayed(function() { + view.trigger('displayed'); + }); }, update: function(){ From a2e9d90bd0be94c03708ec2f98ca70d60991d408 Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Fri, 18 Jul 2014 22:28:08 +0000 Subject: [PATCH 3/4] incorrect comment --- IPython/html/static/widgets/js/widget_container.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/IPython/html/static/widgets/js/widget_container.js b/IPython/html/static/widgets/js/widget_container.js index e545751f0..41af79282 100644 --- a/IPython/html/static/widgets/js/widget_container.js +++ b/IPython/html/static/widgets/js/widget_container.js @@ -37,7 +37,7 @@ define([ var view = this.create_child_view(model); this.$el.append(view.$el); - // Trigger the displayed event once this model is displayed. + // Trigger the displayed event once this view is displayed. this.once_displayed(function() { view.trigger('displayed'); }); @@ -229,7 +229,7 @@ define([ var view = this.create_child_view(model); this.$body.append(view.$el); - // Trigger the displayed event once this model is displayed. + // Trigger the displayed event once this view is displayed. this.once_displayed(function() { view.trigger('displayed'); }); From cd7942eb065b3b4d23bfe985f0c3b4689cbd1d00 Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Wed, 23 Jul 2014 20:26:44 +0000 Subject: [PATCH 4/4] renaming once_displayed into after_displayed --- IPython/html/static/widgets/js/widget.js | 2 +- IPython/html/static/widgets/js/widget_container.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index d916b75ca..26c96562b 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -390,7 +390,7 @@ define(["widgets/js/manager", this.model.save_changes(this.callbacks()); }, - once_displayed: function (callback, context) { + after_displayed: function (callback, context) { // Calls the callback right away is the view is already displayed // otherwise, register the callback to the 'displayed' event. if (this.is_displayed) { diff --git a/IPython/html/static/widgets/js/widget_container.js b/IPython/html/static/widgets/js/widget_container.js index 41af79282..4a570412c 100644 --- a/IPython/html/static/widgets/js/widget_container.js +++ b/IPython/html/static/widgets/js/widget_container.js @@ -38,7 +38,7 @@ define([ this.$el.append(view.$el); // Trigger the displayed event once this view is displayed. - this.once_displayed(function() { + this.after_displayed(function() { view.trigger('displayed'); }); }, @@ -230,7 +230,7 @@ define([ this.$body.append(view.$el); // Trigger the displayed event once this view is displayed. - this.once_displayed(function() { + this.after_displayed(function() { view.trigger('displayed'); }); },