当用户向后和向前单击时,我想给控制器打电话。但是我无法在window.onpopstate中访问我的控制器。我还尝试将控制器置于历史状态数据中,但这会导致错误。
尝试在onpopstate中访问我的控制器:
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(){...}
});
尝试使我的控制器进入状态:
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中的错误:
未捕获的错误:DATA_CLONE_ERR:DOM异常25
然后,如何在window.onpopstate中访问控制器?
还是可以在控制器中向后向和向前事件添加侦听器?
最佳答案
我终于找到了解决方案!在我的控制器中编写以下代码:
window.onpopstate = Ext.bind(this.onPopState, this);
这样可以将功能范围更改为我的控制器,然后可以在onpopstate中调用我的控制器!