How do I get onRendered to run a second time when the route changes but the template remains the same?

本小妞迷上赌 提交于 2020-01-02 05:05:53

问题


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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/29907410/how-do-i-get-onrendered-to-run-a-second-time-when-the-route-changes-but-the-temp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!