PDA

View Full Version : Posting int array back to web service with Json Writer



9 Sep 2011, 3:06 AM
Hi there,
I apologize in advance if this question has a very obvious answer that I'm missing. I'm making use of a Ext.data.Store and a Json reader and I'm able to read from my web-service with no problems. However, I have an array that will mostly contain only one (int) value which I want to send back to my web-service. Every time the user approves/rejects a transaction the id of the transaction needs to be sent back. The question is, creating a model and updating the model every time this happens seems like a bit of over-kill. Also, there is also a function that will populate the array to be sent back with multiple id's. Regardless, is there a way for me to just send the array as an object to the web service?
I've been googling and so far all I'm getting is doing this in conjunction with a store that has a read and write and that makes use of the same model.

Any advise would be greatly appreciated!

mrduck
11 Sep 2011, 2:40 AM
I'm with stupid. There is some great documentation about *getting* data. But what are the recommended methods the write back to the server? Can anyone help us out?

13 Sep 2011, 12:30 AM
I might have found a solution to the problem. Well this solved my problem at least. I have a web service method that accepts an object with 2 List<int> properties (ApprovedNumbers and RejectedNumbers). The model below specifies that:
//Full object
Ext.regModel('ApprovalRequest', {
Fields: [
{name: 'ApprovedNumbers'},
{name: 'RejectedNumbers'}
],
proxy: {
type: 'ajax',
url: '/Approvals/Service/<Method>',
writer: {
type: 'json',
root: 'requests'
}
}
});

In my Js function, I create 2 arrays which I populate, assign to the model fields and call the save() method:

var poAArray = [];
var poRArray = [];
poRArray.push(123);


var approvalRequest = Ext.ModelMgr.create({
ApprovedNumbers: poAArray,
RejectedNumbers: poRArray},
'ApprovalRequest'
&nbsp;);
approvalRequest.save();

The Json data sent to the web-service:
{"requests":[{"ApprovedNumbers":[],"RejectedNumbers":[490183]}]}

Note: The models' field names map exactly to the names of the ws method parameter objects' properties (List<int>). The root field of the model should also map exactly (by name) to the parameter of the called ws method.

Unfortunately this results in an array of obj's being sent to my web-service. I now manage that by simply calling another method from Js and then looping through each entry in the array (which will only contain 1) which calls the original method I wanted to call with one obj only. But for now it works.

I hope this is of some help to anyone out there.