我目前正在开发一个具有许多 View / Controller /...的 Ext JS 应用程序 我想知道加载 JS Controller / View /等等的最佳实践是什么......
目前我的应用程序定义如下:
// enable javascript cache for debugging, otherwise Chrome breakpoints are lost
Ext.Loader.setConfig({ disableCaching: false });
Ext.require('Ext.util.History');
Ext.require('app.Sitemap');
Ext.require('app.Error');
Ext.define('app.Application', {
name: 'app',
extend: 'Ext.app.Application',
views: [
// TODO: add views here
'app.view.Viewport',
'app.view.BaseMain',
'app.view.Main',
'app.view.ApplicationHeader',
//administration
'app.view.administration.User'
...
],
controllers: [
'app.controller.Viewport',
'app.controller.Main',
'app.controller.ApplicationHeader',
//administration
'app.controller.administration.User',
...
],
stores: [
// stores in there..
]
});
这会以某种方式强制客户端在启动时加载我所有的 View 和 Controller ,当然还会调用所有 Controller 的所有初始化方法。 每次更改我的 View 时我都需要加载数据.. 现在我无法在我的 Controller 初始化函数中加载它。我假设我必须做这样的事情:
init: function () {
this.control({
'#administration_User': {
afterrender: this.onAfterRender
}
});
},
有更好的方法吗?或者只是其他事件?
尽管我质疑自己的主要问题是在启动时加载所有 javascript 是否是最佳做法。只加载客户端现在确实需要的 Controller / View /...不是更好吗?或者我应该在启动时加载所有 JS 吗?
如果我确实想动态加载 Controller ,我该怎么做?我假设必须将它们从我的应用程序数组( View 、 Controller 、存储)中删除,并在需要时创建一个实例,然后在 Controller 初始化中设置 View ?!
什么是最佳实践??
更新
带有 View Controller 的 deftJS 会是一个很好的选择吗? deftJS 的主要优点和缺点是什么?
最佳答案
您可以在启动时加载所有 Controller 。请参阅下面的链接,测试了 1000 个 Controller 并且速度很快:
Best practices for initializing and deconstructing controllers
对于快速应用程序,您最好的选择是使用 sencha cmd 将所有必需的文件最小化并压缩到一个文件中。
您不应该在 init
中将数据加载到 View 中,因为 ui 尚未加载,如果您愿意,可以在 onLoad
上加载数据。
我会在 afterrender
或 activated
事件中加载 View 数据。
理想情况下,您将使用模块化方法,在该方法中您在需要该模块的 Controller 、 View 和模型的模块上调用 init()
方法。
关于javascript - Ext JS 4.2.1 加载 Controller - 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280237/