EXT JS 5 - 覆盖ViewController的定义?
问题描述:
我希望我的所有ViewController都有两个自定义方法。EXT JS 5 - 覆盖ViewController的定义?
我试图通过创建一个类,从ViewController
延伸,称为CustomViewController
,再有我的其他ViewControllers致以CustomViewController
类来做到这一点,但后来我得到在控制台的警告消息说:
[W] Overriding existing mapping: 'controller.login' From 'MyApp.view.mybutton.MyButtonController' to 'MyApp.view.override.CustomViewController'. Is this intentional?
而我测试它的组件甚至没有加载。
我意识到我可以直接从我的应用程序的根文件夹中的ext
文件夹内的ext-all-debug.js库执行此操作,但是当我使用Sencha CMD构建应用程序时,它将使用我的原始库那是在我的工作区中,而不是我在应用程序文件夹中的那个,所以我的更改只会在开发时运行,并且不会继续进行生产。
这样做的正确方法是什么?有没有标准?
答
该错误可能意味着您在Eathisa.view.login.loginController
和Eathisa.view.override.EathisaViewController
上都具有相同的alias
配置。当您尝试通过别名使用它时,会加载哪些类,这就是为什么类系统会警告您。
从你的描述来看,听起来好像你根本就不需要重写。如果您需要在您的所有ViewControllers一些方法,你可以在自定义视图控制器添加它们,然后用它作为应用程序中的所有其他ViewControllers基地,而不是Ext.app.ViewController
:
Ext.define('Eathisa.view.AbstractViewController', {
extend: 'Ext.app.ViewController',
// Note that there is no "alias" property here, so that
// this abstract VC can't be instantiated by alias
// You can even make these custom methods excluded from
// production build by enclosing them in the <debug></debug>
// comment brakets:
//<debug>
methodFoo: function() {
...
}
//</debug>
});
Ext.define('Eathisa.view.login.LoginController', {
extend: 'Eathisa.view.AbstractViewController',
alias: 'controller.login',
methodThatUsesFoo: function() {
// Just don't forget to enclose the code that *calls*
// debug-only methods in the same <debug> brackets
//<debug>
this.methodFoo();
//</debug>
...
}
});
如果这不可行从相同的抽象VC扩展所有的ViewController,改为在mixin中实现自定义方法,并将该mixin包含在需要调试方法的VC中:
Ext.define('Eathisa.mixin.Debug', {
methodFoo: function() {
...
}
});
Ext.define('Eathisa.view.login.LoginController', {
extend: 'Ext.app.ViewController',
alias: 'controller.login',
// Conditionally include the debugging mixin
//<debug>
mixins: [
'Eathisa.mixin.Debug'
],
//</debug>
...
});
很好的解释!这就是我想要做的;有一个抽象的控制器,并有其他的扩展,但我可以看到问题是我的CustomViewController的别名。非常感谢你! – Cramps 2014-09-22 21:53:52