-
24 Jul 2012 1:32 AM #1
Answered: REST proxy server response when creating
Answered: REST proxy server response when creating
I'm trying to create a new model on the server, like this:
Is there a sane way of accessing the result that the server sends from within the callbacks provided to model.save()?Code:model = Ext.create("MyApp.model.MyModel", { some: "data" }); model.save({ callback: function (record, operation) { // How to get server response here? // This works sometimes but not always: response = Ext.decode(operation.getResponse().responseText); Ext.Logger.info("Model created, id is " + response.records[0].id); model.setId(response.records[0].id); } })
-
Best Answer Posted by aatiis
Solution seems to be to subscribe to the 'exception' event. Unfortunately the it doesn't seem to be possible to reference the response from the exception event handler, so it needs to be re-read, using proxy.getReader().read(response).getMessage() (and configure messageParam on the reader accordingly).
An easier solution is to defune a custom reader, which is what we did.
-
24 Jul 2012 1:48 AM #2
A little digging around revealed that the callback is fired from
…which does not set the "response" as a parameter when calling any callbacks.Code:Ext.data.proxy.Server.processResponse()
It does however set the parameter when calling the "exception" event, so I guess the answer is to sign up to the "exception" event rather than using callbacks, to handle failures.
This still doesn't solve the problem if there are no exceptions, i.e. we want to get the ID returned from the server.
-
24 Jul 2012 7:21 AM #3
Solution
Solution
Solution seems to be to subscribe to the 'exception' event. Unfortunately the it doesn't seem to be possible to reference the response from the exception event handler, so it needs to be re-read, using proxy.getReader().read(response).getMessage() (and configure messageParam on the reader accordingly).
An easier solution is to defune a custom reader, which is what we did.
-
13 Jan 2013 6:42 PM #4
Yes. The success and failure callbacks are called with two arguments--the new model and operation. The server's response can be found in the operation object:
Code:makeRequest: function (model, callbacks) {Code:var me = this; model.save({ success: function(newModel, operation) { console.log("tryModelHandling callback"); try { var loginResult = me.getResponseData(operation.getResponse()); Cortana.manager.SessionHandler.setToken(loginResult.sessionGuid); Cortana.manager.SessionHandler.setUserAccountGuid(loginResult.userAccountGuid); Cortana.manager.SessionHandler.setEmployeeGuid(loginResult.employeeGuid); // call the callback function passing in the result from the server callbacks.success.call(callbacks.scope, loginResult); } catch (e) { window.console.log('Error in Cortana.manager.Login#makeRequest: ' + e.toString()); } }, failure: function (newModel, operation) { var loginResult = me.getResponseData(operation.getResponse()); callbacks.failure.call(callbacks.scope, loginResult); } });




Reply With Quote