更改Query Param不刷新路由

时间:2017-12-17 22:35:14

标签: ember.js

如果查询参数更改,我已在路由中设置queryParam设置以刷新模型。但是,当查询参数更改setupController时未调用。知道为什么会这样吗?

export default Route.extend({
  queryParams: {
    id: {
      refreshModel: true
    }
  },

  setupController(controller) {
    controller.start(); // fetches data synchronously from api
  }
});

//感谢alptugd的回复解决方案

export default Route.extend({
  queryParams: {
    id: {
      refreshModel: true
    }
  },

  model() {
    // Returning a new array/object/value everytime the hook is called, causes the 
    // setupController hook to be called.
    return [];
  }

  setupController(controller) {
    controller.start(); // fetches data synchronously from api
  }
});

1 个答案:

答案 0 :(得分:4)

refreshModel forces the route to refresh and as expected you should expect beforeModel, model, afterModel, and setupController hooks to run. However; there is a minor difference for setupController hook:

Take a look at the comment for refresh method of Ember's route.js in the source code or relevant API documentation. Both say:

"Refresh the model on this route and any child routes, firing the beforeModel, model, and afterModel hooks in a similar fashion to how routes are entered when transitioning in from other route. The current route params (e.g. article_id) will be passed in to the respective model hooks, and if a different model is returned, setupController and associated route hooks will re-fire as well."

This means; in order for the setupController to trigger in case a refresh occurs; a different model should be returned from the model hook. In your case; you do not have any model hook; hence setupController will not be called upon id param value change.

By the way; it is a good practice to call super method in case you override setupController hook since its sole purpose is to save the model property to the controller. Take a look at the API for a detailed explanation regarding this case.