当路线更改但模板保持不变时,如何使onRendered再次运行? [英] How do I get onRendered to run a second time when the route changes but the template remains the same?

查看:58
本文介绍了当路线更改但模板保持不变时,如何使onRendered再次运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条铁路由器路线,用于更新特定项目的数据:

I have an iron-router route for updating the data of a specific project:

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

每次用户导航到编辑项目页面"时,我都要关注项目名称输入.

Each time the user navigates to an "edit project page" I want to focus the project-name input.

template.onRendered(function() {
  this.$('form input[name="project_name"]').focus();
});

从仪表板导航到任何给定的编辑项目页面时,这非常有用.但是,从一个项目页面导航到另一个项目页面/从另一个项目页面导航到另一页面onRendered函数不会重新运行,因此输入内容没有集中.

This works great when navigating from the Dashboard to any given edit project page. However, navigating to/from one project page to another the onRendered function doesn't rerun and consequently the input is not focused.

推荐答案

您可以强制 onRendered 在之后重新评估通过在 currentData 的检查来进行上下文更改.com/#/full/template_autorun>自动运行,如下所示:

You can force onRendered to reevaluate after a context change by adding a check for currentData inside of an autorun like this:

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();
  });
});

有关更多详细信息,请参见此问题的结尾.

For more details, see the end of this issue.

这篇关于当路线更改但模板保持不变时,如何使onRendered再次运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆