【发布时间】:2013-11-16 08:31:19
【问题描述】:
这是我的路由器的一部分:
this.resource('phoneNumbers', function () {
this.resource('locators', function () {
this.route('index');
this.route('new');
this.route('show', { path: '/:locator_id/show' });
this.route('edit', { path: '/:locator_id/edit' });
});
this.resource('phonelocations', function () {
this.route('index');
});
});
有时我需要做以下事情:
- 用户点击编辑按钮
- 我抓住了当前路径:
phoneNumbers.locators.index - 显示一个编辑表单,带有一个
cancel按钮 - 如果单击取消按钮,我想过渡到旧路线,使用捕获的
currentPath:this.transitionToRoute(oldCurrentPath);
但是transitionToRoute 期待的是locators.index 而不是完整路径phoneNumbers.locators.index:
Assertion failed: The route phoneNumbers.locators.index was not found
Uncaught Error: There is no route named phoneNumbers.locators.index.index
(不知道为什么ember会尝试phoneNumbers.locators.index.index)
这是为什么呢?我怎样才能“映射”到路线的路径?我必须为此维护自己的PATH_TO_ROUTE_MAP 吗?这就是我已经开始做的事情,它有效,但Ember 肯定有更好的方法吗?
另一种方法是捕获当前路线而不是currentPath。这可能吗?怎么样?
【问题讨论】:
-
我相信 Ember 知道定位器是 phoneNumbers 的嵌套资源,因此它不需要完整的路径...请参阅此图表了解如何生成嵌套资源路由名称:emberjs.com/guides/routing/defining-your-routes/…跨度>
-
@MatthewBlancarte:请看我的编辑
-
所以在你的情况下,正确的转换应该是:
transitionToRoute('locators'),因为你是如何命名嵌套资源的。如果您将其更改为this.resource('phoneNumbers.locators', function () {...,它将适用于您的phoneNumbers.locators路线......希望这是有道理的。 :) -
我认为您也可以省略路线中的
index部分。 -
您的意思是,由于我构建路由器的方式,路径和路由在我的应用程序中并不相同。所以我没有配置路由器 right 是我的错误(但是 ember 允许这样做!)我不知道可以像你说的那样命名资源。我会考虑,但我担心更改我的路由器配置会对控制器/模型/路由的命名产生巨大影响,谁知道还有什么其他副作用。
标签: ember.js url-routing