我正在构建一个sencha touch应用程序,其中我有一个在app文件夹之外定义的类,如何动态加载该类。
应用程序结构如下所示:
assets
-> Assignment
-> controller
-> AssignmentAPI
-> Application
-> app
-> controller
-> LoadAssignment
-> app.js
AssignmentAPI:
Ext.define('Assignment.controller.AssignmentAPI', {
extend:'Ext.app.Controller',
statics: {
getAssignments: function() {
}
}
});
LoadAssignment:
Ext.define('Application.controller.LoadAssignment', {
extend:'Ext.app.Controller',
requires: [
],
statics: {
populateAssignments: function() {
Ext.require(['Product.controller.AssignmentAPI']);
var assignmentAPI = Ext.ClassManager.get('Assignment.controller.AssignmentAPI');
//Assignment API is coming as NULL
assignmentAPI.getAssignments()
}
}
});
app.js
Ext.Loader.setPath({
'Ext': 'sdk/src',
'Assignment': '../Assignment'
});
注意*:如果我在RequireAssignment类的requires:[]中指定了Task.controller.AssignmentAPI类,那么它工作正常,但使用Ext.require()加载Assignment.controller.AssignmentAPI不起作用。
答案 0 :(得分:2)
Ext.require()是异步的,所以当它到达你的Ext.ClassManager.get()行时它还没有被包含在内。
您可以使用syncRequire() - 但在类定义中使用“requires”是一种更好的做法。
答案 1 :(得分:0)
Ext.require
结果可能是异步的。你需要在回调中继续你的逻辑:
Ext.require(['Audit.controller.TaskAPI'], function() {
// Here you can be sure that the required class is available
var taskAPI = Ext.ClassManager.get('Task.controller.TaskAPI');
//Task API is coming as NULL
tasksAPI.getTasks()
});
// ... While here, you can't!