javascript - ExtJS:如何在window.onpopstate中调用 Controller ?

标签 javascript extjs history

我想在用户单击后退和前进时调用我的 Controller 。但我无法在 window.onpopstate 中访问我的 Controller 。我还尝试将我的 Controller 放入历史状态数据中,但这会导致错误。

  1. 尝试在 onpopstate 中访问我的 Controller :

    Ext.define('PageController',{
        extend:'Ext.app.Controller',
        onLaunch:function(){
            window.onpopstate = this.onPopState; //trigger the onPopState function when backward and forward
            window.history.pushState(null, '', '/');
        },
        onPopstate:function(s){
            this.someFunction(); //"this" is not PageController, it's PopStateEvent
        },
        someFunction:function(){...}
    });
    
  2. 尝试将我的 Controller 置于状态:

    Ext.define('PageController',{
        extend:'Ext.app.Controller',
        onLaunch:function(){
            window.onpopstate = this.onPopState; //trigger the onPopState function when backward and forward
            window.history.pushState({'controller':this}, '', '/'); //try to put my controller into state data, which causes error
    
        },
        onPopstate:function(s){
            s.state.controller.someFunction();
        },
        someFunction:function(){...}
    });
    

Firebug 中的错误: Uncaught Error :DATA_CLONE_ERR:DOM 异常 25

那么,如何在 window.onpopstate 中访问我的 Controller ? 或者是否可以在我的 Controller 中添加向后和向前事件的监听器?

最佳答案

终于找到解决办法了!在我的 Controller 中编写以下代码:

window.onpopstate = Ext.bind(this.onPopState, this);

这可以将函数作用域更改为我的 Controller ,然后我可以在 onpopstate 中调用我的 Controller !

关于javascript - ExtJS:如何在window.onpopstate中调用 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12703240/

相关文章:

javascript - 过滤器不过滤mat-table中的数据

javascript - 如何在输入文本字段设置值

javascript - 覆盖 ExtJS 5.1.1 中的现有映射

javascript - 将建议值从 ext Combobox 渲染到 DOM 中的元素

javascript - ExtJS XML 字段在 Chrome 中工作,但在 IE 或 FF 中不起作用

javascript - 更改字体大小并修改 Javascript 显示/隐藏切换

javascript数组效率

sql-server - 为什么 SQL Server DATETIME 类型以 1/300 秒的滴答声来节省时间?

linux - bash 命令 history -r 做什么?

history - 'Hello world'从哪里来?