路由之间传递参数 [英] Passing parameters between routes
问题描述
Ember将参数从一个路由发送到另一个路由器的适当方式是什么?例如,我有两条路线定义如下:
this.resource('activities',{path:'/ activities / :on_date'},function(){
this.route('new');
});
当在 ActivitiesRoute
上呈现用户时有可能的活动的下拉菜单。当他们选择的东西转换到 ActivitiesNewRoute
:
this.transitionToRoute ( 'activities.new');
我知道在$ code中有一个第二个参数transitionToRoute(route,模型)方法,但它的意思是传递一个模型,我假设这不应该重用于其他参数传递。在这种情况下,下拉列表选择选择一个 Action
model id,而ActivitiesNew的模型是 Activity
。
以下是我可能工作的三个猜测:
1)使其成为路由器参数
我想我可以更改 ActivitiesNew
在路由中包含一个参数:
this.route('new',{path:'/ new /:my_parameter'});
我不知道我真的想让它成为URL路径的一部分,但如果
2)获取句柄,过渡后
在transitionToRoute调用之后,我可以设置新的控制器类的属性。不知道控制器是否将被安装,但我想象的是:
this.transitionToRoute('activities.new' );
this.get('target')。controllerFor('activities.new')。set('my_parameter',myValue);
3)使用模型参数
this.transitionToRoute('activities.new',myValue);
我怀疑这是一个主要的no-no。我没有研究Ember代码来知道这是否可以工作,但是似乎违反了惯例,所以这是我的坏选择。
您可以使用需要
API(请阅读 here ):
App.ActivitiesNewController = Ember.ObjectController.extend({
需要: ['activities']
//绑定你需要的属性
actionTemplateBinding:'controllers.activities.actionTemplate'
});
所以你实际需要的是在控制器之间传递一个参数,这就是需要
是为。另外,绑定财产与需要
,确保它始终保持同步,而不是依靠 setupController
被调用。
What is the "appropriate" way in Ember to send a parameter from one route to another? For instance, I have two routes defined as such:
this.resource('activities', { path: '/activities/:on_date' }, function() {
this.route('new');
});
when on the ActivitiesRoute
the user is presented with a dropdown of possible activities. When they choose something it transitions to the ActivitiesNewRoute
:
this.transitionToRoute('activities.new');
and I know there is a second parameter available in the transitionToRoute(route,model)
method but it's meant for passing in a model and I'm assuming this shouldn't be repurposed for other parameter passing. In this case the dropdown choice is picking an Action
model id and the model for ActivitiesNew is a Activity
.
Here are my three guesses at ways that might work:
1) Make it a router parameter
I supposed I could change ActivitiesNew
to include a "parameter" as part of the route:
this.route('new', { path: '/new/:my_parameter' });
I'm not sure I'd really like to have it becoming part of the URL path but if this was the prevailing convention then I'd live with that.
2) Get a handle, post transition
Immediately following the transitionToRoute call I could set a property of the new controller class. Not sure if the controller would be setup yet but I'm imagining something like:
this.transitionToRoute('activities.new');
this.get('target').controllerFor('activities.new').set('my_parameter', myValue);
3) Use model parameter
this.transitionToRoute('activities.new',myValue);
I suspect that this is a major no-no. I haven't looked into the Ember code to know if this could work but it seems against convention so this is my "bad option".
You can use the needs
API (Read about it here):
App.ActivitiesNewController = Ember.ObjectController.extend({
needs: ['activities']
// Bind the property you need
actionTemplateBinding: 'controllers.activities.actionTemplate'
});
So what you actually need is to pass a parameter between controllers, which is exactly what needs
is for. Plus, binding the property with needs
you ensure it is in sync at all times, instead of relying on setupController
being called.
这篇关于路由之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!