[英]execute code only when the “base” route is activated in emberjs
Is there a way to execute when a "base" route is activated/re-activated? 激活/重新激活“基本”路由时,是否有执行方法? I have the following urls:
我有以下网址:
Router.map(function() {
// /projects
this.resource('projects', function() {
// /projects/2
this.resource('project', {path: ':id'});
});
});
I'd like to transitionTo
the individual project route if there is only one project returned from the projects
route. 如果项目路线中仅返回一个项目,我想
transitionTo
到单个projects
路线。 I can do this: 我可以做这个:
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.findAll('project');
},
afterModel: function(projects, transition) {
if (projects && projects.get('length') === 1) {
this.transitionTo('project', projects.get('firstObject'));
}
}
});
This works nicely...except i can still go to the projects
route and since the model has already been loaded that hook doesn't fire again. 这很好用...除非我仍然可以转到
projects
路线,并且由于模型已经加载,所以钩子不会再次触发。 Is there a way to listen to always enforce that rule when the projects
route is activated? 当
projects
路线激活时,有没有办法监听始终执行该规则的方法?
For example, if i go to /projects/23
and then i go to /projects
i'd like to auto-transition them to the single project route if there is only one. 例如,如果我转到
/projects/23
,然后转到/projects
我想将它们自动转换为单个项目路由(如果只有一个)。 I don't know how to accomplish that since it's a nested route and the activate
and afterModel
methods have already been fired when visiting /projects/23
for the first time. 我不知道该怎么做,因为这是一条嵌套路线,并且第一次访问
/projects/23
时已经activate
了activate
和afterModel
方法。
Does that make sense? 那有意义吗? and how can i accomplish this?
我怎么能做到这一点?
Use an index route on your projects resource. 在项目资源上使用索引路由。 It will only get hit when you hit
/projects
仅当您命中
/projects
时才会命中
ProjectsIndex ProjectsIndex
export default Ember.Route.extend({
model: function() {
var projects = this.modelFor('projects');
if (projects && projects.get('length') === 1) {
this.transitionTo('project', projects.get('firstObject'));
}
},
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.