Emberjs:根据动态段重置控制器

时间:2021-10-03 21:10:24

I have this route structure in my app:

我在我的应用程序中有这个路由结构:

Profile.Router.map(function() {
  this.resource('profile', { path: '/:user' }, function(){
    this.route('followers');
  });
});

My /:user/followers page is supposed to show the list of followers of :user. The controller for profile.followers is setup for infinite scroll - so it has properties like curPage, resultsPerPage.

我/:用户/关注者页面应该显示以下关注者列表:user。 profile.followers的控制器设置为无限滚动 - 因此它具有curPage,resultsPerPage等属性。

Profile.ProfileFollowersController = Ember.ArrayController.extend(InfiniteScroll.ControllerMixin,{
  curPage: 1,
  resultsPerPage: 20,
  //other code for infinite scroll which increments curPage as user scrolls down
})

Now, since controllers are singleton in emberjs, when I move from /tom/followers to /jin/followers, the properties for infinite scroll that were set for /tom/followers get preserved and applied for /jin/followers as well.

现在,由于控制器是emberjs中的单例,当我从/ tom / followers移动到/ jin / followers时,为/ tom / followers设置的无限滚动属性也会被保留并应用于/ jin / followers。

For eg. say I am on /tom/followers and scroll down 4 pages, curPage property of 'profile.followers' controller gets set as 4. Now when I move to /jin/followers, though the model hook of the route would return list of followers for jin, but would pick curPage as 4 since ember's controllers are singleton and I had scrolled down to 4th page on /tom/followers. How is this supposed to be handled?

例如。说我在/ tom / followers并向下滚动4页,'profile.followers'控制器的curPage属性设置为4.现在当我移动到/ jin / followers时,虽然该路线的模型钩子会返回关注者列表对于jin,但是会选择curPage为4,因为ember的控制器是单例,我已经向下滚动到/ tom / followers上的第4页。怎么应该处理?

Here is my profile.followers route as well:

这是我的profile.followers路线:

Profile.ProfileFollowersRoute = Ember.Route.extend({
  model: function(params) {
    console.log("fetching followers model");
    return Ember.$.getJSON('/'+this.modelFor('profile').user+'/followers?new=1');
  },
});

1 个解决方案

#1


1  

You can use the route's setupController hook for this. This hook is fired every time the route is entered.

你可以使用route的setupController钩子。每次输入路线时都会触发此挂钩。

App.ProfileFollowersRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    // Call _super for default behavior
    this._super(controller, model);
    // Reset the desired controller properties
    controller.setProperties({
      curPage: null,
      someOtherProp: null
    });
  }
});

I've made a jsbin demonstrating it: http://emberjs.jsbin.com/fiyetefeno/6/

我做了一个jsbin演示它:http://emberjs.jsbin.com/fiyetefeno/6/

You can read the API documentation here: http://emberjs.com/api/classes/Ember.Route.html#method_setupController

您可以在此处阅读API文档:http://emberjs.com/api/classes/Ember.Route.html#method_setupController

#1


1  

You can use the route's setupController hook for this. This hook is fired every time the route is entered.

你可以使用route的setupController钩子。每次输入路线时都会触发此挂钩。

App.ProfileFollowersRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    // Call _super for default behavior
    this._super(controller, model);
    // Reset the desired controller properties
    controller.setProperties({
      curPage: null,
      someOtherProp: null
    });
  }
});

I've made a jsbin demonstrating it: http://emberjs.jsbin.com/fiyetefeno/6/

我做了一个jsbin演示它:http://emberjs.jsbin.com/fiyetefeno/6/

You can read the API documentation here: http://emberjs.com/api/classes/Ember.Route.html#method_setupController

您可以在此处阅读API文档:http://emberjs.com/api/classes/Ember.Route.html#method_setupController