REFACTOR: Use component for `cook-text` to improve async workflow

This commit is contained in:
Robin Ward 2017-06-29 12:14:27 -04:00
parent aa1a68b979
commit 6ff7bf7505
4 changed files with 17 additions and 19 deletions

View File

@ -0,0 +1,15 @@
import { cookAsync } from 'discourse/lib/text';
const CookText = Ember.Component.extend({
tagName: '',
cooked: null,
didReceiveAttrs() {
this._super(...arguments);
cookAsync(this.get('rawText')).then(cooked => this.set('cooked', cooked));
}
});
CookText.reopenClass({ positionalParams: ['rawText'] });
export default CookText;

View File

@ -1,18 +0,0 @@
import { cookAsync } from 'discourse/lib/text';
import guid from 'pretty-text/guid';
import { registerUnbound } from 'discourse-common/lib/helpers';
function cookText(text) {
const id = `${guid().replace(/-/g, '')}`;
cookAsync(text)
.then(cooked => {
Em.run.next(()=>{
$('#' + id).html(cooked.string);
});
});
return new Handlebars.SafeString(`<div id='${id}'></div>`);
}
registerUnbound('cook-text', cookText);

View File

@ -0,0 +1 @@
{{cooked}}

View File

@ -34,7 +34,7 @@
{{#if editing}} {{#if editing}}
{{d-editor value=buffered.raw}} {{d-editor value=buffered.raw}}
{{else}} {{else}}
{{{cook-text post.raw}}} {{cook-text post.raw}}
{{/if}} {{/if}}
</div> </div>