Extjs如何定义一个全局常量文件
Posted
技术标签:
【中文标题】Extjs如何定义一个全局常量文件【英文标题】:Extjs how to define a global constants file 【发布时间】:2019-10-29 19:51:30 【问题描述】:从here 和here 以及其他一些谷歌搜索中,我可以看到始终在代码中定义的全局常量是可能的。我需要用这些常量声明一个 XML 或 JSON 文件,这样如果我需要更改这些常量,就不会更改源代码。如何使用 Extjs 实现这一点?
【问题讨论】:
【参考方案1】:在您的项目根目录中,您可以创建一个类似“data/”的目录并在其中添加一个 JSON 文件,例如 constants.json
。
然后,当您的应用启动时,您可以向该文件发出 ajax 请求并将其存储在某处,例如单例。
Constants.js:
Ext.define('MyApp.Constants',
alternateClassName: ['Constants'],
singleton: true,
SOME_CONSTANT: 'Abc', // you can also add values that are not part of the file
TIMEOUT: null // if you like to make it clear what values you are expecting, from constants.json
);
某处,可能在 RootController
Ext.Ajax.request(
url: 'localhost:1841/data/constants.json',
method: 'GET'
).then(function (result)
var data = Ext.Json.decode(result.responseText);
Ext.Object.each(data, function (key, value)
Constants[key] = value; // assuming all key-values in result are valid constants...
)
)
数据/常量.json:
"TIMEOUT": 60000,
"FOO": "BAR"
【讨论】:
以上是关于Extjs如何定义一个全局常量文件的主要内容,如果未能解决你的问题,请参考以下文章