PDA

View Full Version : RestProxy and create (ie. POST)



abierbaum
16 Dec 2010, 12:47 PM
What does RestProxy expect to be the return from a server when a new object is created using POST?

Can it handle a 201 (Created) status with the Location header containing a link to the new resource or does it require the response body to contain a complete representation of the newly created resource?

-Allen

abierbaum
17 Dec 2010, 9:13 AM
From what I have found so far, the response text from POST and PUT requests must be a record of the same item that you just created or updated.

The code in question is in Model.save in the callback. If the request is successful, the response will automatically be read for records and then there is a call to "me.set(record.data)" to update the saved Model with any changes from the server. If you don't return anything, then an exception will be thrown at this location because it can't access "record.data" on an undefined value.

IMHO, this behavior makes sense for create actions (POST in my case), but for a PUT operation I don't see why there needs to be a copy of the record returned. I think the code should be able to handle the case of no record returned and still treat it as a success.

edspencer
20 Dec 2010, 10:38 AM
201 should work as well as 200 - can you try the following override to see if it fixes your problem?



Ext.override(Ext.data.Reader, {
read: function(response) {
var data = response;

if (response && response.responseText) {
data = this.getResponseData(response);
}

if (data) {
return this.readRecords(data);
} else {
return this.nullResultSet;
}
}
});

samHassan
25 Jan 2011, 2:56 AM
I was getting the same error but this code didn't help as the server was returning:

server said:

{"ErrorCode":"0","Message":"OK","Data":null}

error using V1.0.1a was:
Uncaught TypeError: Cannot read property 'Results' of null
anonymous:2
Ext.data.Reader.Ext.extend.readRecords sencha-touch-debug.js:9457
Ext.data.JsonReader.Ext.extend.readRecords sencha-touch-debug.js:9750
Ext.override.read SenchOverrides.js:10
(anonymous function) sencha-touch-debug.js:8707
Ext.data.Connection.Ext.extend.onCompletesencha-touch-debug.js:17495
Ext.data.Connection.Ext.extend.onStateChange sencha-touch-debug.js:17442
(anonymous function)sencha-touch-debug.js:3421

SenchOverrides.js is whereI had your code.

so I made a small change to your code and it works fine:


Ext.override(Ext.data.Reader, {
read: function(response) {
var data = response;

if (response && response.responseText) {
data = this.getResponseData(response);
}

if (data.Data) {
return this.readRecords(data);
} else {
return this.nullResultSet;
}
}
});

Thanks Ed!

- I really like the fact you can override a function from anywhere with Ext.override

edspencer
25 Jan 2011, 2:02 PM
@sam I think if you update your Reader's root to "Data", you can use the original override

twilkinson
5 Jul 2011, 9:13 AM
I thought I would post this here since my problem is similar to the one above. I'm struggling with what the server should return from a PUT operation (proxy: type:'rest').

In all the following cases, the PUT is generated when I call the model.save() function.

CASE 1: If the PUT returns:



{success;:true,data:[]}
I get an exception in the save: function (line 7727 of sencha-touch-debug-w-comments.js) at:



record = operation.getRecords()[0];
....
me.set(record.data);

since 'record' is undefined: a couple of lines above, operation.getRecords() has returned an empty array and the indexing [0] is consequently undefined.

CASE 2: If the PUT returns:



{success:true}
I get an exception in the readRecords function (line 15523 of sencha-touch-debug-w-comments.js) at:



var root = this.getRoot(data),
total = root.length,
since 'root' is undefined and therefore root.length fails.

CASE 3: If the PUT returns a copy of the entire uploaded data record, then things work okay, but some of my records are rather large (base64 encoded images) and the overhead is killing me.

I would have expected the library to handle the case where the PUT operation was successful and it was not necessary to return any more data.

What am I doing wrong?

Thanks for any help,
Terry