From f9243a10cc5c88f95eaa94573633c3fe27ae88af Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 6 Dec 2013 17:13:28 -0500 Subject: [PATCH] Don't debouce `afterRender`, debounce the preview refreshing mechanism. --- .../discourse/views/composer/composer_view.js | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/discourse/views/composer/composer_view.js b/app/assets/javascripts/discourse/views/composer/composer_view.js index 8621f22e2a6..9dee7733362 100644 --- a/app/assets/javascripts/discourse/views/composer/composer_view.js +++ b/app/assets/javascripts/discourse/views/composer/composer_view.js @@ -48,22 +48,24 @@ Discourse.ComposerView = Discourse.View.extend(Ember.Evented, { return this.present('controller.createdPost') ? 'created-post' : null; }.property('model.createdPost'), - observeReplyChanges: function() { - var self = this; - if (this.get('model.hidePreview')) return; - Ember.run.next(function() { - if (self.editor) { - self.editor.refreshPreview(); - // if the caret is on the last line ensure preview scrolled to bottom - var caretPosition = Discourse.Utilities.caretPosition(self.wmdInput[0]); - if (!self.wmdInput.val().substring(caretPosition).match(/\n/)) { - var $wmdPreview = $('#wmd-preview'); - if ($wmdPreview.is(':visible')) { - $wmdPreview.scrollTop($wmdPreview[0].scrollHeight); - } + + refreshPreview: Discourse.debounce(function() { + if (this.editor) { + this.editor.refreshPreview(); + // if the caret is on the last line ensure preview scrolled to bottom + var caretPosition = Discourse.Utilities.caretPosition(this.wmdInput[0]); + if (!this.wmdInput.val().substring(caretPosition).match(/\n/)) { + var $wmdPreview = $('#wmd-preview'); + if ($wmdPreview.is(':visible')) { + $wmdPreview.scrollTop($wmdPreview[0].scrollHeight); } } - }); + } + }, 30), + + observeReplyChanges: function() { + if (this.get('model.hidePreview')) return; + Ember.run.scheduleOnce('afterRender', this, 'refreshPreview'); }.observes('model.reply', 'model.hidePreview'), movePanels: function(sizePx) { @@ -131,7 +133,7 @@ Discourse.ComposerView = Discourse.View.extend(Ember.Evented, { }, // Called after the preview renders. Debounced for performance - afterRender: Discourse.debounce(function() { + afterRender: function() { var $wmdPreview = $('#wmd-preview'); if ($wmdPreview.length === 0) return; @@ -156,7 +158,7 @@ Discourse.ComposerView = Discourse.View.extend(Ember.Evented, { }); this.trigger('previewRefreshed', $wmdPreview); - }, 100), + }, initEditor: function() { // not quite right, need a callback to pass in, meaning this gets called once,