以ember访问浏览历史记录 [英] Access browsing history in ember
问题描述
我有以下两条路径来编辑我的应用程序中的对象(节点):
I have the following two paths to edit an object (node) in my application:
- 列表节点 - >点击编辑图标
- 列出节点 - >选择节点 - >点击编辑按钮
我可以选择取消编辑。当我取消,我想要路由器(控制器?)返回到正确的地方。也就是说,如果我来自节点列表,我想取消返回到节点列表( #nodes
)。如果我来自节点视图,我想取消返回到节点视图(#nodes / show / node-id
)。目前这是我的 NodesEditController
中的实现:
I have the option to cancel the editing. When I cancel, I want the router (controller?) to go back to the right place. That is, if I came from the list of nodes, I want cancel to go back to "list of nodes" (#nodes
). If I came from the node view, I want cancel to go back to the node view (#nodes/show/node-id
). Currently this is the implementation in my NodesEditController
:
SettingsApp.NodesEditController = Ember.ObjectController.extend({
needs: ['nodesIndex'],
selectedNode: null,
selectedNodeType: null,
nodes: [],
...
cancel: function () {
this.stopEditing();
this.transitionToRoute('nodes.show', this.get('content'));
},
...
});
如您所见,的路线取消
动作是固定的( nodes.show
)。但是在第一种情况下,我想做 this.transitionToRoute('nodes.index');
。所以我的取消
方法必须是这样的:
As you can see, the route for the cancel
action is fixed (nodes.show
). But in the first case, I would like to do this.transitionToRoute('nodes.index');
. So my cancel
method must be something like this:
cancel: function () {
this.stopEditing();
if (some_test) { this.transitionToRoute('nodes.show', this.get('content'));
} else { this.transitionToRoute('nodes.index'); }
}
如何实现 some_test
?
推荐答案
重新打开 Ember.Route
在路线退出时存储 currentPath
:
Reopen Ember.Route
to store the currentPath
when a route is exited:
Ember.Route.reopen({
deactivate: function() {
var applicationController = this.controllerFor('application');
App.previousPath = applicationController.get('currentPath');
}
});
然后在您的取消方法中:
Then in your cancel method:
goBack: function () {
if (SettingsApp.previousPath == 'nodes.show') {
this.transitionToRoute(SettingsApp.previousPath, this.get('content'));
} else {
this.transitionToRoute(SettingsApp.previousPath);
}
},
cancel: function () {
this.stopEditing();
this.goBack();
},
注意:您可能希望提供一些备用,以防应用程序加载在 nodes.edit
路线。
Note: you might want to provide some fallback in case the app is loaded in the nodes.edit
route.
这篇关于以ember访问浏览历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!