【问题标题】:Access browsing history in ember在 ember 中访问浏览历史记录
【发布时间】:2013-06-17 07:47:02
【问题描述】:

我有以下两条路径可以在我的应用程序中编辑对象(节点):

  1. 列出节点 -> 点击编辑图标
  2. 列出节点->选择节点->点击编辑按钮

我可以选择取消编辑。当我取消时,我希望路由器(控制器?)回到正确的位置。也就是说,如果我来自节点列表,我想取消返回“节点列表”(#nodes)。如果我来自节点视图,我想取消返回节点视图 (#nodes/show/node-id)。目前这是我NodesEditController中的实现:

SettingsApp.NodesEditController = Ember.ObjectController.extend({
    needs: ['nodesIndex'],
    selectedNode: null,
    selectedNodeType: null,
    nodes: [],
    ...
    cancel: function () {
        this.stopEditing();
        this.transitionToRoute('nodes.show', this.get('content'));
    },

    ...
});

如您所见,cancel 操作的路径是固定的 (nodes.show)。但在第一种情况下,我想做this.transitionToRoute('nodes.index');。所以我的cancel 方法一定是这样的:

 cancel: function () {
    this.stopEditing();
    if (some_test) { this.transitionToRoute('nodes.show', this.get('content'));
    } else { this.transitionToRoute('nodes.index'); }
 }

some_test如何实现?我可以测试什么来了解我是如何到达当前路线的?

【问题讨论】:

    标签: testing ember.js url-routing html5-history


    【解决方案1】:

    在退出路由时重新打开Ember.Route 以存储currentPath

    Ember.Route.reopen({
      deactivate: function() {
        var applicationController = this.controllerFor('application');
        App.previousPath = applicationController.get('currentPath');
      }
    });
    

    然后在你的取消方法中:

    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 路由中加载,您可能需要提供一些回退。

    【讨论】:

    • 不应该是Ember.Route.reopen吗?我可以在我的应用程序的任何地方这样做吗?抱歉,这里是新手。
    • 谢谢,成功了。确实是Ember.Route.reopen。我对您的回复稍作修改,使其更加完整。
    猜你喜欢
    • 2013-05-21
    • 2010-09-08
    • 2010-09-08
    • 1970-01-01
    • 2015-06-14
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多