Ember.js Emberjs:基于动态段的控制器复位

Ember.js Emberjs:基于动态段的控制器复位,ember.js,Ember.js,我的应用程序中有此路由结构: Profile.Router.map(function() { this.resource('profile', { path: '/:user' }, function(){ this.route('followers'); }); }); 我的/:user/followers页面应该显示:user的追随者列表。profile.followers的控制器设置为无限滚动-因此它具有类似curPage、resultsPerPage的属性 Profil

我的应用程序中有此路由结构:

Profile.Router.map(function() {
  this.resource('profile', { path: '/:user' }, function(){
    this.route('followers');
  });
});
我的
/:user/followers
页面应该显示
: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
})
现在,由于控制器在emberjs中是单例的,当我从
/tom/followers
移动到
/jin/followers
时,为
/tom/followers
设置的无限滚动属性也会被保留并应用于
/jin/followers

例如,假设我在
/tom/followers
上,向下滚动4页,
'profile.followers'
控制器的
curPage
属性设置为4。现在,当我移动到
/jin/followers
,虽然路线的模型钩子会返回jin的追随者列表,但会选择
curPage
作为4,因为余烬的控制器是单例的,我已经在
/tom/followers
上向下滚动到第4页。 这应该怎么处理

这是我的
档案。追随者
路线:

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

为此,您可以使用
路由
设置控制器
挂钩。每次进入路线时都会触发此钩子

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
    });
  }
});
我制作了一个jsbin来演示它:

您可以在此处阅读API文档:

谢谢您的努力!但这是正确的余烬处理方式吗?我不明白为什么控制器是一个单体,如果我每次都要重置各种属性。非常像余烬的方式。控制器状态保存在内存中,直到您通过重新加载或自行重置来刷新它。这家伙解释得更清楚了: