在sencha文件中找不到关于这个问题的任何相关信息:
是否可以使用不依赖于应用程序名称的参数调用Ext.create(...)
?
那么,如果我更改应用程序的名称,我不必重写那行代码?
通常情况下,我会使用Ext.create(AppName.model.MYMODEL)
,但这与我的应用程序名称相关联。
仍然需要帮助:)
答案 0 :(得分:6)
使用Ext.define
定义班级时,您可以提供alias
属性。您可能已经在使用widget.panel
等别名的UI组件上看到了这一点。这些别名可以与Ext.create
一起使用。
Ext.define('MyApp.SomeClass', {
alias: 'app.someclass', // Independent of class name
/* ... */
});
Ext.create('app.someclass', {
/* ... */
});
您可以在使用Ext.ClassManager.setAlias
创建类别后设置别名。
如果您没有设置别名的选项,则可以创建一个包装Ext.create
的函数,该函数自动提供基本命名空间。
这里的问题是Ext.application
不返回应用程序对象。我不确定Sencha Architect如何生成应用程序代码,但您可能需要额外的覆盖才能检索应用程序对象。
function appCreate(className, config) {
var appName = someMethodThatGetsTheApplicationName();
return Ext.create(appName + '.' + className, config);
};
// Example usage: Creates object of MyApp.model.MyModel
var myObj = appCreate('model.MyModel', { /* ... */ });
默认情况下,Ext JS在使用Ext.application
时不保留对应用程序对象的引用,因此我们需要覆盖才能执行此操作。我使用Ext.currentApp
作为存储此对象的属性,但您可以将其更改为您想要的任何内容。
Ext.application = function (config) {
Ext.require('Ext.app.Application');
Ext.onReady(function () {
Ext.currentApp = new Ext.app.Application(config);
});
};
现在您已拥有此功能,只需使用Ext.currentApp.name
即可访问应用程序名称。或者,如果您使用吸气剂感觉更舒服,可以使用以下物品。
Ext.app.Application.addMembers({
getName: function () {
return this.name;
}
});
// Example usage:
function someMethodThatGetsTheApplicationName() {
if (!Ext.currentApp) {
Ext.Error.raise('Current app does not exist.');
}
return Ext.currentApp.getName();
}
答案 1 :(得分:1)
您可以在Ext.create
中使用任何类名,只要已经定义了类,就没有强制命名约定。如果您希望Ext.create
使用Ext.loader
加载正确的文件,则需要配置加载程序以符合您需要的命名约定。
答案 2 :(得分:1)
做到这一点的方法:
你需要一个控制器,它将在 INIT功能(在UI加载/启动之前)执行以下操作
APPNAME = this.getApplication().getName();
APPNAME是全局变量。
然后当你Ext.create的东西,你将能够写下以下
Ext.create(APPNAME +'model.MyModel');
通过这种方式,您可以更改应用名称,而无需检查代码中的任何位置,以将每个Ext.create更改为新应用的名称。
如果你要使用this.getApplication()。setName()来拥有无限的缓存存储,你可以获得每个AppName 5 / 10mb的能力。