流星适当经常使用

Meteor proper constant use

本文关键字:常使用 流星      更新时间:2023-09-26

我在文本区域使用WYSIWYG (bootstrap3-wysiwyg5)包。这个包可以正常工作,但是每当渲染页面时,用粗体、斜体等修改文本的菜单就会成倍增加。

是这样的

我认为使用{{#constant}}块会阻止这一点,但它似乎没有做任何事情。

那么,两个问题:

  1. 在这个实例中我是否错误地使用了{{#constant}}块?
  2. 如果没有,有什么建议停止这样的渲染情况吗?
下面

代码:

要使用所见即所得编辑器,只需将其添加到您想要使用的任何文本区域。在客户端我有:

Template.announcements.rendered = function(){
  $('#announcement_message').wysihtml5({
    "font-styles": false, //Font styling, e.g. h1, h2, etc. Default true
    "emphasis": true, //Italics, bold, etc. Default true
    "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
    "html": false, //Button which allows you to edit the generated HTML. Default false
    "link": true, //Button to insert a link. Default true
    "image": false, //Button to insert an image. Default true,
    "color": false //Button to change color of font
  });;
};

然后在模板中添加:

{{#constant}}
  <textarea id="announcement_message" class="form-control" name="message" type="text"></textarea>
{{/constant}}

constant块通常以这种方式工作,但您的问题在于渲染函数实际运行的次数。如果你在你的Template.announcements.rendered()里放一个console.log(),我敢打赌它会出现4次。

这在Meteor的新渲染系统Blaze中不会是一个问题。他们正在解决这个问题。

Ben McMahen有一个关于Meteor的渲染回调的伟大的帖子,在那,他有一个建议的代码块,以确保某些东西只运行一次,它是通过附加一个rendered属性到模板实例,像这样:

Template.announcements.rendered({
   if(!this.rendered) {
      // This code only runs once
      $('#announcement_message').wysihtml5({
         "font-styles": false, //Font styling, e.g. h1, h2, etc. Default true
         "emphasis": true, //Italics, bold, etc. Default true
         "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
         "html": false, //Button which allows you to edit the generated HTML. Default false
         "link": true, //Button to insert a link. Default true
         "image": false, //Button to insert an image. Default true,
         "color": false //Button to change color of font
      });;
      this.rendered = true;
   }
});