meteor - 当路由更改但模板保持不变时,如何让 onRendered 再次运行?

标签 meteor iron-router

我有一个用于更新特定项目数据的 Iron-router 路由:

Router.route('/project/:key/update', {
  ...
});

每次用户导航到“编辑项目页面”时,我都希望关注项目名称输入。
template.onRendered(function() {
  this.$('form input[name="project_name"]').focus();
});

从仪表板导航到任何给定的编辑项目页面时,这非常有效。但是,从一个项目页面导航到另一个项目页面 onRendered函数不会重新运行,因此输入没有聚焦。

最佳答案

您可以强制onRendered通过添加对 currentData 的检查在上下文更改后重新评估内部 autorun像这样:

Template.myTemplate.onRendered(function() {
  var self = this;
  this.autorun(function() {
    // hack to force the autorun to reevaluate
    Template.currentData();

    // insert onRendered code here
    self.$('form input[name="project_name"]').focus();
  });
});

详情见this issue文末.

关于meteor - 当路由更改但模板保持不变时,如何让 onRendered 再次运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907410/

相关文章:

javascript - Meteor - 只发布集合的计数

javascript - meteor 发现不返回数据

javascript - 在 kadira :flow-router for meteor 上使用 react-router 有什么好处

javascript - Meteor 路由、Pub/Sub

javascript - 将 div 链接到路由 meteor (Iron Router)

meteor - 在 Iron 路由器中设置属于用户的数据

javascript - 使用来自 Iron-router 服务器路由的数据渲染模板

javascript - 子类化 Meteor.users() 以获得不同的用户类型

javascript - 无法将新数组 $push 到 Meteor Collection 中的文档中

meteor - Meteor JS 中的简单计时器