PDA

View Full Version : Proxy extraParams



hugodelgado
14 Apr 2014, 6:53 AM
Hi, i'm using extjs 4.2.1 and having problems with extraParams property on a store proxy. The proxy is a direct proxy and before i call the load on the store for the first time i add a extraParam like this:

proxy.setExtraParam('xpto', 'value');

On the next calls to load in the store, the param is added correctly, without doing anything more. This store is used on a grid with cellEdit plugin and if i edit a record the update request sent to server doesn't contains the defined extraParam on the store proxy.

I've tried to see with the event beforesync if the store proxy contains the extraParams and it does, but the param isn't sent. Is this the usual way of working? From what i see in the docs under the ext.data.proxy.server the extraParams config says:

Extra parameters that will be included on every request.

I'm not sure if i'm missing something, has anyone had a similar problem?

thanks

Gary Schlosberg
23 Apr 2014, 10:37 AM
Looks like extraParams are included only for read requests:
http://docs.sencha.com/extjs/4.2.1/#...fg-extraParams (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.proxy.Direct-cfg-extraParams)

hugodelgado
20 May 2014, 6:44 AM
I'm not sure if it the best way, but since extraParams aren't sent on every requests a way to implement that functionality could be this one. It is just extending a jsonWriter and before sending the requests it adds the extraParams to every data object.

Cheers


Ext.define('XPTO.data.writer.ExtraParams', {
extend: 'Ext.data.writer.Json',
alternateClassName: 'XPTO.data.ExtraParams',
alias: 'writer.xpto-extraparams',

addExtraParamsToData: function(params, data, single) {
if (single) {
Ext.iterate(params, function(key, value) {
data[key] = value;
}, this);
} else {
Ext.iterate(data, function(entry, index) {
Ext.iterate(params, function(key, value) {
data[index][key] = value;
}, this);
}, this);
}
},

writeRecords: function(request, data) {
var root = this.root;

if (this.expandData) {
data = this.getExpandedData(data);
}

var single = this.allowSingle && data.length === 1;
if (single) {
// convert to single object format
data = data[0];
}
this.addExtraParamsToData(request.params, data, single);

if (this.encode) {
if (root) {
// sending as a param, need to encode
request.params[root] = Ext.encode(data);
} else {
//<debug>
Ext.Error.raise('Must specify a root when using encode');
//</debug>
}
} else {
// send as jsonData
request.jsonData = request.jsonData || {};
if (root) {
request.jsonData[root] = data;
} else {
request.jsonData = data;
}
}
return request;
}
});

fabio.policeno
30 Jun 2015, 1:17 PM
I'm not sure if it the best way, but since extraParams aren't sent on every requests a way to implement that functionality could be this one. It is just extending a jsonWriter and before sending the requests it adds the extraParams to every data object.

Cheers


Ext.define('XPTO.data.writer.ExtraParams', {
extend: 'Ext.data.writer.Json',
alternateClassName: 'XPTO.data.ExtraParams',
alias: 'writer.xpto-extraparams',

addExtraParamsToData: function(params, data, single) {
if (single) {
Ext.iterate(params, function(key, value) {
data[key] = value;
}, this);
} else {
Ext.iterate(data, function(entry, index) {
Ext.iterate(params, function(key, value) {
data[index][key] = value;
}, this);
}, this);
}
},

writeRecords: function(request, data) {
var root = this.root;

if (this.expandData) {
data = this.getExpandedData(data);
}

var single = this.allowSingle && data.length === 1;
if (single) {
// convert to single object format
data = data[0];
}
this.addExtraParamsToData(request.params, data, single);

if (this.encode) {
if (root) {
// sending as a param, need to encode
request.params[root] = Ext.encode(data);
} else {
//<debug>
Ext.Error.raise('Must specify a root when using encode');
//</debug>
}
} else {
// send as jsonData
request.jsonData = request.jsonData || {};
if (root) {
request.jsonData[root] = data;
} else {
request.jsonData = data;
}
}
return request;
}
});

Thanks!

I have created my override with params:

Ext.define('Overrides.view.override.data.writer.Json', {
override: 'Ext.data.writer.Json',

addApiExtraParams: function(params, data, single) {
var me = this;


if (single) {
Ext.iterate(params, function(key, value) {
data[key] = value;
}, me);
} else {
Ext.iterate(data, function(entry, index) {
Ext.iterate(params, function(key, value) {
data[index][key] = value;
}, me);
}, me);
}
},


writeRecords: function(request, data) {
var me = this,
root = me.getRootProperty(),
json, single, transform;


if (me.getExpandData()) {
data = me.getExpandedData(data);
}

if (me.getAllowSingle() && data.length === 1) {
// convert to single object format
data = data[0];
single = true;
}

transform = this.getTransform();
if (transform) {
data = transform(data, request);
}


me.addApiExtraParams(request.getProxy().apiExtraParams[request.getAction()], data, single);

if (me.getEncode()) {
if (root) {
// sending as a param, need to encode
request.setParam(root, Ext.encode(data));
} else {
//<debug>
Ext.Error.raise('Must specify a root when using encode');
//</debug>
}
} else if (single || (data && data.length)) {
// send as jsonData
json = request.getJsonData() || {};
if (root) {
json[root] = data;
} else {
json = data;
}
request.setJsonData(json);
}
return request;
}
});

Using:


...
proxy: {
type: 'ajax',
apiExtraParams: {
create: {
opcode: 'test'
}
},
...

fabio.policeno
1 Jul 2015, 4:23 AM
Ext.define('Overrides.view.override.data.writer.Json', {
override: 'Ext.data.writer.Json',


setApiParams: function(request, data) {
var me = this,
root = me.getRootProperty(),
apiExtraParams = request.getProxy().apiExtraParams || {},
params = apiExtraParams[request.getAction()],
single = me.getAllowSingle() && data.length === 1,
json;


Ext.iterate(params, function(key, value) {
if(me.getEncode() && root) {
request.setParam(key, value);
}
else if(single || (data && data.length)) {
json = request.getJsonData() || {};

if(root) {
json[key] = value;
}
else {
json = data;
request.setParam(key, value);
}


request.setJsonData(json);
}
});
},


writeRecords: function(request, data) {
this.callParent(arguments);
this.setApiParams(request, data);


return request;
}
});

Using:


...
proxy: {
type: 'ajax',
apiExtraParams: {
create: {
myParam: 'myValue'
},
update: {
...
},
...
},
api: {
...
}
...
}
...