我正在学习EXTJS框架,对于我在前端侧面ExtJ和后端侧Javaee javaee Spring框架上使用的实验(将其配置为REST服务)。
因此,我在Localhost:1841和Back-End部分开始了前端部分:Localhost:8080。问题是:
我如何向Extjs.Store说请求需要发送到 Localhost:8080/**而不是本地主机:1841/**?
对不起我的英语!
在Extjs中 singleton
您可以使用此类。
singleton 模式是一种设计模式,仅将类的实例化限制为一个对象。当整个系统中需要一个对象时,这很有用。Singleton模式提供了对特定实例的访问点。单例模式的实现必须满足单个实例和全局访问原则。
为此"我如何对extjs.store说请求需要发送到localhost:8080/**而不是localhost:1841/**?"
您可以在您的应用中使用如下代码:
首先创建
singletone
类
/*
* Create singletone class in your application
* This class you can access in your application anywhere you want within the app.
* Usage:
* commonUtility.getServerUrl();// whatever property you have defined inside of config you can access like this
*/
Ext.define('APPNAME.utils.SingleToneClassName', {
alternateClassName: 'commonUtility',
singleton: true,
config: {
/*
* you can put local or live also or whatever you want.
* for local it will be ip address like this {'http://192.168.30.83:8080/'}
* for live is will be live tomcate host url {http://example.com/}
*/
serverURL: 'http://192.168.30.83:8080/'
},
constructor: function(config) {
var me = this;
me.initConfig(config);
},
});
创建/定义您的商店
//Your store
Ext.define('APPNAME.store.StoreName', {
extend: 'Ext.data.Store',
fields: ['your fields here'],
storeId: 'storeIdHere',
alias: "store.storeAliasHere",
proxy: {
type: 'ajax',
url: commonUtility.getServerUrl() + 'your Server Method name here', //Based on your server URL acceptance
withCredentials: true,
reader: {
type: 'json',
rootProperty: 'data',
keepRawData: true
}
},
autoLoad: true, //If you need auto load then put true otherwise false
listeners: {
beforeload: function(store, operation, options) {
//If you have token based authenthication then you need to put like below
store.getProxy().setHeaders({
"x-auth-token": 'your token here'
});
//If you have need to pass some parameter in API method then you can pass like below
store.getProxy().extraParams.your_parameter_name = 'value';
}
},
});
//If you want to load your store on some event or any other functions
//then
Ext.getStore('your_storeId_herer').load({
url: commonUtility.getServerUrl() + 'your Server Method name here', //Based on your server URL acceptance
params: {
//If you have need to pass some params in server side then
//you put here like
name: 'value'
}
});
我希望这对您有帮助。有关更多详细信息,您可以参考 extjs6.x docs