Javascript 如何在beforeload事件上获取Extjs 4商店的请求数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7492689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get Extjs 4 store's request data on beforeload event?
提问by Gihan Lasita
I'm trying to get request data params beforeload event on store. I can see the operation object contains the request data but I can't seems to get it from operation object
我正在尝试在商店的加载事件之前获取请求数据参数。我可以看到操作对象包含请求数据,但我似乎无法从操作对象中获取它
Ext.create('Ext.data.Store', {
autoLoad : true,
fields : [
{name: 'item_code', type: 'string'},
{name: 'quantity', type: 'int'},
{name: 'description', type: 'string'}
],
storeId : 'summary',
proxy : {
type : 'ajax',
actionMethods : 'POST',
extraParams : {'filter': 'branch', 'branch': location },
url : 'reports/stock_summary',
reader: {
type : 'json',
root : 'data'
}
},
listeners: {
beforeload: function(store, operation, options){
console.log( operation )
}
}
});
any idea how to get request object on before load event and get data inside of that request object?
知道如何在加载事件之前获取请求对象并获取该请求对象中的数据吗?
采纳答案by Daniel
how to try operation.request.params
如何尝试 operation.request.params
i think this could help you ...
我想这可以帮助你...
var test = Ext.create('Ext.data.Store', {
autoLoad : true,
fields : [
{name: 'item_code', type: 'string'},
{name: 'quantity', type: 'int'},
{name: 'description', type: 'string'}
],
storeId : 'summary',
proxy : {
type : 'ajax',
actionMethods : 'POST',
extraParams : {'filter': 'branch', 'branch': location },
url : 'reports/stock_summary',
reader: {
type : 'json',
root : 'data'
}
},
listeners: {
beforeload: function(store, operation, options){
console.log( 'manual load ' + operation.params );
console.log( operation.params );
console.log( 'proxy defined params ' + store.proxy.extraParams );
console.log( store.proxy.extraParams )
}
}
});
test.load({params: {test: '123'}});
回答by Johnny Rice
The proxy is what makes the request and has all of the related request data. You can override the doRequest method of the Ext Ajax Proxy. The correct way to do this is to create a new custom proxy but for brevity here is your sample updated to override doRequest in the proxy. You can then fire an event that passes the data out that you want to anything bound to the new event or you can write your logic inline where the console.log current is as this example is coded.
代理发出请求并拥有所有相关的请求数据。您可以覆盖 Ext Ajax 代理的 doRequest 方法。执行此操作的正确方法是创建一个新的自定义代理,但为简洁起见,此处已更新您的示例以覆盖代理中的 doRequest。然后,您可以触发一个事件,将您想要的数据传递给绑定到新事件的任何内容,或者您可以在 console.log 当前所在的位置编写您的逻辑内联,如本示例所编码。
Ext.create('Ext.data.Store', {
autoLoad : true,
fields : [
{name: 'item_code', type: 'string'},
{name: 'quantity', type: 'int'},
{name: 'description', type: 'string'}
],
storeId : 'summary',
proxy : {
type : 'ajax',
actionMethods : 'POST',
extraParams : {'filter': 'branch', 'branch': location },
url : 'reports/stock_summary',
reader: {
type : 'json',
root : 'data'
},
/*
* override Ext Ajax Proxy doRequest method
* must be maintained when Ext library is updated in the app
*/
doRequest: function(operation, callback, scope) {
var writer = this.getWriter(),
request = this.buildRequest(operation, callback, scope);
if (operation.allowWrite()) {
request = writer.write(request);
}
Ext.apply(request, {
headers : this.headers,
timeout : this.timeout,
scope : this,
callback : this.createRequestCallback(request, operation, callback, scope),
method : this.getMethod(request),
disableCaching: false // explicitly set it to false, ServerProxy handles caching
});
/*
* do anything needed with the request object
*/
console.log('request', request);
console.log('request.params', request.params);
Ext.Ajax.request(request);
return request;
}
}});
回答by Tracker1
Can't speak for extjs4, but in extjs3 on a prior project, I had to supress and override a function in ext..
不能说 extjs4,但在先前项目的 extjs3 中,我不得不抑制和覆盖 ext..
(function(){
var originalFn = path.to.functionNAme
path.to.functionName = function(...) {
//frob data here
originalFn(...);
}
})
Not the best answer, as I don't have that codebase, was about 2 years ago on another job.
不是最好的答案,因为我没有那个代码库,是大约 2 年前的另一份工作。