Emberjs:查询参数的更改未反映在控制器属性中

时间:2014-12-03 18:39:08

标签: ember.js

我在控制器中绑定了一个查询参数:

Profile.MoviesController = Ember.ArrayController.extend({
  queryParams: ['type'],
  type:"horror,action,romantic,biography",
});

我正在寻找路线中查询参数的变化:

Profile.MoviesRoute = Ember.Route.extend({
  actions: {
    queryParamsDidChange: function(params) {
      console.log("queryParamsDidChange",params,this.get('controller').get('type'));
    }
  }
});

最后,在我看来,我使用了每个'类型'的链接:

{{linkTo "Horror" (query-params type="horror")}}, {{linkTo "Action" (query-params type="action")}} ...

问题在于每当我点击任何链接时 - 说'恐怖'一个 - 然后调用queryParamsDidChange挂钩而params参数包含{type:'horror'},但是{{1}控制器上的属性 - 'type' - 仍为this.get('controller').get('type')。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

在您的控制器中,您将“type”的默认值设置为字符串值“horror,action,romantic,biography” - 这与您想要的任何内容都不匹配。

接下来,在type属性上设置值之前调用queryParamsDidChange,例如

App = Ember.Application.create();

App.Router.map(function() {  
});

App.IndexRoute = Ember.Route.extend({  
  actions: {
    queryParamsDidChange: function(params) {
       console.log("queryParamsDidChange : was " + this.get('controller.type')); 
       console.log("queryParamsDidChange : will be " + (params.type || 'default value'));
    }
  }
});

App.IndexController = Ember.ArrayController.extend({
  queryParams: ['type'],
  type: "horror"  // the default value, won't appear in URL or params
})

这个工作示例的jsbin是http://emberjs.jsbin.com/jeqijoguku/1/