如何使用Ember.js将Highlight.js应用于新视图?

如何使用Ember.js将Highlight.js应用于新视图?,ember.js,highlight.js,Ember.js,Highlight.js,我试图使用highlight.js进行语法高亮显示,但只能在第一次加载时使其运行 HTML: <script type="text/x-handlebars" id="post"> {{#if html}} {{rawhtml html}} {{else}} {{#each section}} {{rawhtml this}} {{/each}} {{/if}} </script> 演示:转到代码,然后是“CSS最佳实践”。刷新浏览器。将显

我试图使用highlight.js进行语法高亮显示,但只能在第一次加载时使其运行

HTML:

<script type="text/x-handlebars" id="post">
 {{#if html}}
   {{rawhtml html}}
 {{else}}
   {{#each section}}
   {{rawhtml this}}
   {{/each}}
 {{/if}}
</script>
演示:转到代码,然后是“CSS最佳实践”。刷新浏览器。将显示语法高亮显示(粉红色和紫色)。导航到“命名文件”并返回。现在代码返回到白色

JSBIN:

注意:我尝试使用{{each}}助手的无块形式,但没有成功

<script type="text/x-handlebars" id="post">
 {{each controller postViewClass="App.PostView"}}
</script>

App.PostView = Ember.View.extend({
  template: Ember.Handlebars.compile('{{#if html}}{{rawhtml html}}{{else}}{{#each section}}{{rawhtml this}}{{/each}}{{/if}}'),
    didInsertElement: function () {
      hljs.initHighlighting();
    }
  });

{{each controller postViewClass=“App.PostView”}
App.PostView=Ember.View.extend({
模板:Ember.handlebar.compile({{{{if-html}}{{rawhtml-html}}{{{else}}{{{each section}{{rawhtml-this}}{{/each}{{/if}}}),
didInsertElement:函数(){
hljs.initHighlighting();
}
});
并且由于某种原因不能使用
this.$().hljs.initHighlighting()


我开始感到困惑,所以任何帮助都将不胜感激!谢谢

为此,我认为您需要使用计算属性。只需在“Post”类中定义一个返回正确值并后跟.property扩展名的函数:

然后把它放在模板中:
{{{{{htmlContent}}


你说你不能使用
this.$().hljs.initHighlighting(),但你不说为什么?控制台中的消息?

我认为这里的问题是,ember代码与“jquery插件”样式的代码混合不好。 highlight.js似乎有一些同步功能可用,只需获取一些代码并用正确的突出显示标记即可。
hljs.highlightAuto('code')

我将尝试创建一个高亮显示Handlebar帮助程序,它使用hljs函数处理文本,如:

{{highlight model.html}

意识到问题与highlight.js无关。我加了警告(‘工作!’);代替hljs.initHighlighting();我只收到一次提示。
<script type="text/x-handlebars" id="post">
 {{each controller postViewClass="App.PostView"}}
</script>

App.PostView = Ember.View.extend({
  template: Ember.Handlebars.compile('{{#if html}}{{rawhtml html}}{{else}}{{#each section}}{{rawhtml this}}{{/each}}{{/if}}'),
    didInsertElement: function () {
      hljs.initHighlighting();
    }
  });
htmlContent: function() {
    var html= this.get('html');
    if (blah blah blah) {
        // do what you need to do to return the new content
    }
    return newValueToDisplay;
}.property('html')