Javascript ExtJS:如何访问Ajax请求&x27;来自任何其他代理的s值?
我正在从一个web服务获取一些特定数据,需要将这些数据传输到另一个web服务的Javascript ExtJS:如何访问Ajax请求&x27;来自任何其他代理的s值?,javascript,ms-access,extjs,proxy,Javascript,Ms Access,Extjs,Proxy,我正在从一个web服务获取一些特定数据,需要将这些数据传输到另一个web服务的extraParams。我怎样才能做到这一点 我创建了一个singleton类,该类处理信息并在获取这些数据之后进行处理;另一个商店代理将使用这些数据 这是独生子女班 Ext.define('MyApp.MyInfo', { requires: [] , singleton: true, getMyId: function () { var me = this;
extraParams
。我怎样才能做到这一点
我创建了一个singleton
类,该类处理信息并在获取这些数据之后进行处理;另一个商店代理将使用这些数据
这是独生子女班
Ext.define('MyApp.MyInfo', {
requires: [] ,
singleton: true,
getMyId: function () {
var me = this;
var request = Ext.Ajax.request({
url: myUrl() + '/info', //Here is web-service url
success: function(response, opts) {
var obj = Ext.decode(response.responseText);
var myId = obj.data[0].id; // Successfully gets required ID
console.log(myId); // Successfully prints ID value
// callback(userGid); //Not sure if callback method will handle ID value to fetch on any other class..
},
failure: function(response, opts) {
console.log('server-side failure with status code ' + response.status);
}
});
}
});
这里是另一个代理
,它需要myId
来自Ajax的值
stores: {
myFooStore: {
model: 'MyApp.Model',
autoLoad: true,
session: true,
proxy: {
url: myUrl() + '/the/other/service',
type: 'ajax',
extraParams: {
id: // HERE! I need to get myId value on here. And couldn't get it.
},
reader: {
type: 'json',
rootProperty: 'data'
}
}
},
您需要调用方法来加载特定的存储,而不是存储
自动加载。您可以使用如下示例传递参数:-
//you can set using get proxy
store.getProxy().setExtraParams({
id: id
});
store.load()
//or you can direcly pass inside of store.load method like this
store.load({
params: {
id: 'value' //whatever your extraparms you can pass like this
}
});
在此中,我根据您的要求创建了一个演示。我希望这将有助于/指导您实现您的要求
代码片段
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.define('MyInfo', {
alternateClassName: "myinfo",
singleton: true,
getMyId: function (callback) {
var me = this;
Ext.Ajax.request({
url: 'id.json', //Here is web-service url
success: function (response, opts) {
var obj = Ext.decode(response.responseText),
myId = obj.data[0].id; // Successfully gets required ID
console.log(myId); // Successfully prints ID value
callback(myId);
},
failure: function (response, opts) {
callback('');
console.log('server-side failure with status code ' + response.status);
}
});
}
});
Ext.define('MyViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.myview',
onLoadButtonTap: function () {
var view = this.getView(),
myFooStore = this.getViewModel().getStore('myFooStore');
view.mask('Please wait..');
myinfo.getMyId(function (id) {
view.unmask();
if (id) {
myFooStore.getProxy().setExtraParams({
id: id
});
myFooStore.load();
/*
You can also do like this
myFooStore.load({
url:'',//If you want to change url dynamically
params:{
id:'value'//whatever your extraparms you can pass like this
}
});
*/
}
});
}
});
Ext.define("ViewportViewModel", {
extend: "Ext.app.ViewModel",
alias: 'viewmodel.myvm',
stores: {
myFooStore: {
fields: ['name', 'email', 'phone'],
proxy: {
type: 'ajax',
url: 'data1.json',
reader: {
type: 'json',
rootProperty: ''
}
}
}
}
});
//creating panel with GRID and FORM
Ext.create({
xtype: 'panel',
controller: 'myview',
title: 'Demo with singletone class',
renderTo: Ext.getBody(),
viewModel: {
type: 'myvm'
},
layout: 'vbox',
items: [{
xtype: 'grid',
flex: 1,
width: '100%',
bind: '{myFooStore}',
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}]
}],
tbar: [{
text: 'Load Data',
handler: 'onLoadButtonTap'
}]
});
}
});
谢谢你的回复。我还有一个问题;我已经可以通过var myId=obj.data[0].id从MyInfo
类中获取myId值
那么我可以在第二个存储区上用类似的方法获取这些数据<代码>外部参数:{id:MyApp.MyInfo.getMyId().myId},
。通过这种方式,我将动态加载基于ID的存储,并通过调用变量
本身?或类似的方法来实现所需的数据;外部参数:{id:MyApp.MyInfo.getMyId().success(myId)}
?回调(myId)
方法将在MyInfo
singleton类上工作。如果您正在创建动态存储,那么它是可以的,但是如果您已经在任何类或视图模型中定义了存储,那么无论何时设置extraParams:{id:MyApp.MyInfo.getMyId().myId}
如果值为find,则它有效,否则无效。为此,您可以尝试附加参数:{id:MyApp.MyInfo.getMyId()。成功(myId)}
可能有效,但我不确定是否需要检查一次。它始终失败;正如您在我的代码片段中所注意到的,我声明了MyInfo
类,然后具有getMyId
函数。它包括request
变量来创建ajax请求,然后myId
变量停留在success
函数中。所以我试过这个MyApp.MyInfo.getMyId().request.success(myId)
并返回为request property undefined
。也试过这个,MyApp.MyInfo.getMyId().success(myId)
然后说success属性未定义
。。。还没拿到我的身份证。