emberjs使用多个路径/ url为一个路由 [英] emberjs using multiple paths / urls for one route

查看:91
本文介绍了emberjs使用多个路径/ url为一个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  App.Router.map(function(){
this。 route('accomodations');
});

所以如果一个/ accomodations将加载该视图。



我还可以添加:

  App.Router.map(function(){
this.route('accomodations',{path:'/ travel'});
});

所以如果一个人去/旅行,它会去同一个视图。



我想要拥有/住宿和/旅行去同一个景观?这是可能的吗?



我知道这一点:

  Router.map(function(){
this.route('accomodations');
this.route('accomodations',{path:'/ travel'});
});

会做我所要求的,但如果他们去住宿,它应该显示在url,它总是显示旅行。我不确定最后一段代码是否是最佳做法。

解决方案

使用重定向



router.js

 应用程序.Router.map(function(){
this.route('accomodations');
this.route('travel');
});

routes / travel.js

  App.TravelRoute = Ember.Route.extend({
redirect:function(){
this.transitionTo ('accomodations');
},
});

您不必将这些放在单独的文件中(这就是我放在这些文件中) / p>

这样做是用路由器注册两条路由。选择其中一个是主要路由,另一个是别名路由。在这种情况下, accomodation 是主要路线,而旅行是其别名。当用户访问 / travel 时,他们将被重定向到 / accomodation



这将是默认/标准Ember的完成方式,如果这听起来不错,请继续。



另一种可能的解决方案h3>

如果您不希望重定向发生,由于某些原因,并希望用户看到的URL保持不变,但仍然显示相同的东西,并且行为同样的方式,这也是可以的。



在这种情况下,您将创建每个单个Ember单元中的两个(路由,控制器,视图,模板)。聪明的方法是创建基类路由,并且同时具有 App.TravelRoute App.AccomodationRoute 它;创建基类控制器,并且同时具有 App.TravelController App.AccomodationController 简单地扩展它;相同的观点,如果你有。



模板,OTOH,有点棘手,因为没有办法扩展(我知道)。所以你需要做的是创建一个部分或一个组件(决定哪个更适合你),然后在 templates / accomodation.hbs templates / travel.hbs


In Ember I can use this:

App.Router.map(function() {
    this.route('accomodations');
});

so if one goes to /accomodations it will load that view.

I can also add:

App.Router.map(function() {
    this.route('accomodations', { path: '/travel' });
});

so if one goes to /travel, it will go to the same view.

I want to be able to have /accomodations and /travel go to the same view? is this possible?

I know that this:

App.Router.map(function() {
    this.route('accomodations');
    this.route('accomodations', { path: '/travel' });
});

Will do what I'm asking, but if they go to accommodations, it should show that in the url, it always shows travel. I'm not even sure if the final piece of code is best practice.

解决方案

Using redirection

In router.js

App.Router.map(function() {
    this.route('accomodations');
    this.route('travel');
});

In routes/travel.js

App.TravelRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('accomodations');
    },
});

You do not have to put these in separate files (that's just where I would put them).

What this does is register two routes with the router. Pick one of them to be the "main" route, and the other the "alias" route. In this case, accomodation is the main route, and travel is its alias. When the user visits /travel, they get redirected to /accomodation.

This would be the default/ standard Ember way of accomplishing this, and if this sounds good to you, go for this.

Another possible solution

If you do not wish to have redirection happen, for some reason, and want the URL seen by the user to stay the same, but still display the same things, and behave in the same way, this is also possible.

In this case, you would create two of every single Ember unit (route, controller, view, template). The smart way would be to create a base class route, and have both App.TravelRoute and App.AccomodationRoute trivially extend it; create a base class controller, and have both App.TravelController and App.AccomodationController trivially extend it; same for views, if you have them.

Templates, OTOH, are a little trickier, because there is not way to extend them (that I know of). SO what you would need to do, is create either a partial or a component (decided which works better for you), and then reuse that partial/ component in both templates/accomodation.hbs and templates/travel.hbs

这篇关于emberjs使用多个路径/ url为一个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆