在ember js中,我想知道使用模型钩子中通常可用的路径参数的常用方法是什么?
有时我发现我需要访问控制器内的一个参数;让我们说由于某个动作或某事而发出ajax请求。现在通常你会在模型钩子中返回一个通常也有id的模型。但我有一些案例,我不需要获得模型,只需要价值。在这种情况下,我可以返回一个对象或值作为“模型”,但它似乎有点奇怪。有没有办法直接在控制器中访问params而不是传递值?您可以从控制器访问当前路由数据吗?
另一件事;我有一个需要两个动态段的路线。所以我创建了一个包含路由的资源。现在,在我的路线中,我需要访问这两个参数。我是否必须通过模型钩子传递它们?然后在子控制器中我必须使用“需求”来访问父控制器的内容?这似乎有点太多的解决方法,它使事情变得非常混乱和不清楚
添加到此...如果我通过transitionToRoute之类的东西将一些数据传递给路径,它会跳过路径上的模型方法。但那时你就不再能够获得路线的参数了。您是否希望在setupController中调用模型上的serialize方法来找出你的参数?
答案 0 :(得分:1)
您必须使用目前只能在金丝雀构建中激活的查询参数。
该主题的参考资料可在此处找到:
https://github.com/emberjs/ember.js/pull/3182
这样做的一般方法是:
App.Router.map(function() {
this.resource('posts', {queryParams: ['sort', 'direction']}, function() {
this.resource('post', {path: "/:id", queryParams: ['showDetails']});
});
});
App.IndexRoute = Ember.Route.extend({
beforeModel: function( transition, queryParams ) {},
model: function( params, transition, queryParams ) {},
afterModel: function( resolvedModel, transition, queryParams ) {},
setupController: function( controller, context, queryParams ) {},
renderTemplate: function( controller, context, queryParams ) {}
});
/// transitionTo now will accept a final argument, which must be an object with the key queryParams.
this.transitionTo('post', object, {queryParams: {showDetails: true}});
this.transitionTo('posts', {queryParams: {sort: 'title'}});
this.transitionTo({queryParams: {direction: 'asc'}});
/// You can also use add query params to URL transitions as you would expect:
this.transitionTo("/posts/1?sort=date&showDetails=true");