Meteor 0.8 Blaze如何更新Jquery插件的渲染更改

时间:2014-04-01 15:06:53

标签: javascript jquery meteor footable meteor-blaze

我的问题是如何在DOM中更新一组元素时获得1个事件或渲染回调?如果我按照Blaze wiki https://github.com/avital/meteor-ui-new-rendered-callback中的链接,这不是我想要的。如果我遵循第二个建议,我将获得尽可能多的渲染调用,因为我有元素。并且父元素只会在页面加载时获得1个渲染回调。

就我而言,我正在使用Footable Jquery插件来格式化表格。初始加载工作正常,但是如果我在Collection find中更改过滤器变量,则DOM会更新并且不会再次调用渲染,因为Blaze只调用一次渲染。我不想把它放到另一个模板中,因为这只是对渲染的多次调用,因此当它只需要一个整个表时,就会多次调用Footable。

感谢任何帮助。

<template name="customerData">
  <table class="table">
    {{#each dataRows}}
    <tr>
      <td>{{first_name}}</td>
      <td>{{last_name}}</td>
      <td>{{email}}</td>
     {{#each phones}}
        <td>{{phone}}</td>
     {{/each}}
    </tr>
    {{/each}}
  </table>
</template>

Template.customerData.rendered = function(){
  $(".table").footable();
}

Template.customerData.phones = function(){
    var result = [];

    _.each(this.phoneNumbers, function(element, index, list){
       result.push({ phone: element});
    });

return result;
}

1 个答案:

答案 0 :(得分:7)

最好的解决方案是编写自定义块帮助程序。 Lemme为你做的事:):

实施

UI.registerHelper('footableBody', function () {

  var dependency = new Deps.Dependency(),
      dataSource = this,
      handle, footable;

  return UI.Component.extend({
    render: function () {
      var self = this;
      return UI.Each(function () {
        return dataSource;
      }, UI.block(function () {
        dependency.changed();
        return self.__content;
      }));
    },
    rendered: function () {
      var $node = $(self.firstNode).closest('table');
      handle = Deps.autorun(function () {
        if (!footable) {
          $node.footable();
          footable = $node.data('footable');
        } else {
          footable.redraw();
        }
        dependency.depend();
      });
    },
    destroyed: function () {
      handle && handle.stop();
    },
  });
});

用法

现在,您可以在模板中执行以下操作:

<table class="table">
  <thead>
    ...
  </thead>
  <tbody>
  {{#footableBody dataRows}}
    <tr>
      <td>{{first_name}}</td>
      <td>{{last_name}}</td>
      <td>{{email}}</td>
      <td>{{phone}}</td>
    </tr>
  {{/footableBody}}
  </tbody>
</table>

当然,您应该根据自己的需要自定义帮助程序的行为。

思考

还有另一种 - 更通用的 - 解决方案遵循markdown帮助程序的实现模式here。但是,我不鼓励将此策略应用于您的特定用例。

相关问题