mik*_*mik 6 singleton extjs extjs4
我想在Ext JS 4中创建一个单例对象.
这是我的代码:
Ext.define('AM.model.Test', {
extend : 'Ext.util.Observable',
singleton : true,
foo : function() {
console.log('log 1');
},
constructor : function() {
}
});
Run Code Online (Sandbox Code Playgroud)
我叫它:
AM.model.Test.foo();
Run Code Online (Sandbox Code Playgroud)
只要我在app.js中定义define,一切正常.当我尝试将此定义移动到'app\model\Test.js'时,我收到此错误:
AM.model.Test is undefined [Break On This Error]
AM.model.Test.foo();
我的代码出了什么问题?
您需要设置动态加载,并要求您打算使用它的类.
例如:
Ext.Loader.setConfig({
enabled: true,
paths: {
'AM': 'app'
}
});
Ext.require('AM.model.Test');
Ext.onReady(function() {
// now all the required classes are loaded and you can start using them
AM.model.Test.foo();
});
Run Code Online (Sandbox Code Playgroud)