Javascript Sencha Touch全局变量
我正在开发一个Sencha Touch应用程序,我学习它很棒,因为我喜欢JavaScript 这是我的app.jsJavascript Sencha Touch全局变量,javascript,sencha-touch,Javascript,Sencha Touch,我正在开发一个Sencha Touch应用程序,我学习它很棒,因为我喜欢JavaScript 这是我的app.js var App = new Ext.Application({ name: 'My First App', //BaseURL: 'http://mydomain.com/testing/first/services/', launch: function() { this.views.viewport = new this.views.
var App = new Ext.Application({
name: 'My First App',
//BaseURL: 'http://mydomain.com/testing/first/services/',
launch: function() {
this.views.viewport = new this.views.Viewport();
// this.BaseURL = "http://mydomain.com/testing/first/services/";
}
});
这是我的一家商店
var newsStore = new Ext.data.Store({
model: 'News',
sorters: [{
property: 'PostedOn',
direction: 'DESC'
}],
proxy: {
type: 'ajax',
url: 'http://mydomain.com/testing/first/services/News.php',
reader: {
type: 'xml',
root: 'News',
record: 'New'
}
},
getGroupString: function(record) {
if (record && record.data.PostedOn) {
return record.get('PostedOn').toDateString();
}
else {
return '';
}
},
autoLoad: true
});
现在的问题是,我是否可以在整个应用程序中创建一个全局变量?它名为BaseURL,我可以在所有数据存储中使用它,当需要更改它时,我只需更改它以反映整个应用程序
我需要知道两件事
您可以像不使用Sencha一样声明全局变量:
var BaseURL = "http://mydomain.com/testing/first/services/";
然后使用它:
proxy: {
type: 'ajax',
url: BaseUrl + 'News.php',
reader: {
type: 'xml',
root: 'News',
record: 'New'
}
}
编辑:
如果要将其声明为应用程序实例的成员,请执行以下操作:
var App = new Ext.Application({
name: 'My First App',
launch: function() {
this.views.viewport = new this.views.Viewport();
this.BaseURL = "http://mydomain.com/testing/first/services/";
}
});
然后:
proxy: {
type: 'ajax',
url: App.BaseUrl + 'News.php',
reader: {
type: 'xml',
root: 'News',
record: 'New'
}
}
我建议将自定义全局变量添加到应用程序命名空间,如下所示:
Ext.application({
name: 'MyApp',
launch: function() { },
apiToken: 'foo'
});
这样,在应用程序启动后,您将能够访问这些自定义变量:
MyApp.app.apiToken
它也适用于以下功能:
Ext.application({
name: 'MyApp',
launch: function() { },
apiToken: 'foo',
getApiToken: function() { return this.apiToken; }
});
回答,也许对你有帮助。
特别是,如果您想将参数传递到存储。在一些sencha更新之后,我们可以这样做:
var App = Ext.application({
name: 'Myapp',
serviceUrl: 'https://example',
launch: function() {
}
});
Ext.Ajax.request({
url: Myapp.app.servideUrl,
withCredentials: true,
useDefaultXhrHeader: true,
method: 'post',
scope: this,
params: {
cmd: 'connect',
id: login,
password: pass,
version: 1
},
success: function (response) {
var result = Ext.JSON.decode(response.responseText);
if (result.result.status === 'connected'){
this.loginSucess(result);
}else{
this.loginFail(result);
}
},
failure: function (response) {
this.loginFail();
}
});
内部应用程序实例?我想在应用程序实例内部执行此操作,并通过应用程序实例访问它。另请参阅Ext.namespace()和Ext.Application文档。“name”属性不应包含空格,并且可以用作全局标识符。