Don't debouce `afterRender`, debounce the preview refreshing mechanism.

This commit is contained in:
Robin Ward 2013-12-06 17:13:28 -05:00
parent 9a24d2651d
commit f9243a10cc
1 changed files with 18 additions and 16 deletions

View File

@ -48,22 +48,24 @@ Discourse.ComposerView = Discourse.View.extend(Ember.Evented, {
return this.present('controller.createdPost') ? 'created-post' : null; return this.present('controller.createdPost') ? 'created-post' : null;
}.property('model.createdPost'), }.property('model.createdPost'),
observeReplyChanges: function() {
var self = this; refreshPreview: Discourse.debounce(function() {
if (this.get('model.hidePreview')) return; if (this.editor) {
Ember.run.next(function() { this.editor.refreshPreview();
if (self.editor) {
self.editor.refreshPreview();
// if the caret is on the last line ensure preview scrolled to bottom // if the caret is on the last line ensure preview scrolled to bottom
var caretPosition = Discourse.Utilities.caretPosition(self.wmdInput[0]); var caretPosition = Discourse.Utilities.caretPosition(this.wmdInput[0]);
if (!self.wmdInput.val().substring(caretPosition).match(/\n/)) { if (!this.wmdInput.val().substring(caretPosition).match(/\n/)) {
var $wmdPreview = $('#wmd-preview'); var $wmdPreview = $('#wmd-preview');
if ($wmdPreview.is(':visible')) { if ($wmdPreview.is(':visible')) {
$wmdPreview.scrollTop($wmdPreview[0].scrollHeight); $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'), }.observes('model.reply', 'model.hidePreview'),
movePanels: function(sizePx) { movePanels: function(sizePx) {
@ -131,7 +133,7 @@ Discourse.ComposerView = Discourse.View.extend(Ember.Evented, {
}, },
// Called after the preview renders. Debounced for performance // Called after the preview renders. Debounced for performance
afterRender: Discourse.debounce(function() { afterRender: function() {
var $wmdPreview = $('#wmd-preview'); var $wmdPreview = $('#wmd-preview');
if ($wmdPreview.length === 0) return; if ($wmdPreview.length === 0) return;
@ -156,7 +158,7 @@ Discourse.ComposerView = Discourse.View.extend(Ember.Evented, {
}); });
this.trigger('previewRefreshed', $wmdPreview); this.trigger('previewRefreshed', $wmdPreview);
}, 100), },
initEditor: function() { initEditor: function() {
// not quite right, need a callback to pass in, meaning this gets called once, // not quite right, need a callback to pass in, meaning this gets called once,