PDA

View Full Version : access operation.response when success : false



dedoz
16 Apr 2012, 9:17 AM
im using Ext 4.1rc2 and model/store with a Direct proxy.

my goal: get the message property in the response when sucess is false.

problem : when success is true, i can access operation.response.message and get that server message, but when success is false, operation.response its undefined so i cant get the message and tell the user what server said (why request failed)

server request can be any from model.load store.load model.save etc.
The model proxy has its reader root config set to data (note that everthing is working when server responde success:true and all records are loaded correctly )

my JSON response is


{ "type":"rpc",
"tid":1,
"action":"empQuery",
"method":"Insert",
"result":{ // comes inside result because Direct proxy reads "result" or "data" by default
"data":{"empId":"294","empRut":"11","empRazon":"test11"},
"success":true,
"message":"ok"
}
}

when operation fails in the server side i return a success:false and message property contains a message from server.


client side values when JSON response success : true


some_model_instance.save({
callback:function(record,operation) {
console.log(operation.wasSuccessful()); // true
console.log(operation); // displays the object
console.log(operation.response); // display the object with all response properties
}
});

client side values when JSON response success : false


some_model_instance.save({
callback:function(record,operation) {
console.log(operation.wasSuccessful()); // false
// detects correclty the success:false from the JSON
console.log(operation); // displays the object
console.log(operation.response); // undefined
}
});


so how can i access the message property when success: false ? why operation.response is undefined when success is false ? should i use another property to retrieve messages when success its false ?

dedoz
16 Apr 2012, 12:41 PM
Solution
after painful hours reading source code :d

a message property MUST be set to the proxy reader, theres no default value and if you dont set it, a message in the response will be ignored

so the model proxy definition must be something like this


Ext.define('emp', {
extend: 'Ext.data.Model',
fields: [
{name:'empId' ,type:'int' },
{name:'empRut' ,type:'string' },
{name:'empRazon' ,type:'string' }
],
idProperty: 'empId',
proxy:
{ type: 'direct',
api:{ create : empQuery.Insert},
reader : {
root:'data',
messageProperty : 'message'
}

}
});


besides that, when success is set to false in the JSON response, the operation wont have a response property because if success is false ill will create an exception with a message if we set that messageProperty config.

this comes from this source


processResponse: function(success, operation, request, response, callback, scope) {
......
if (result.success !== false) {
Ext.apply(operation, {
//if success:true then creates the response property
response: response,
resultSet: result
});
......
} else {
//if success : false then put the message on a exception
operation.setException(result.message);
me.fireEvent('exception', this, response, operation);
}
.......
},

note that result.message only be there if we configure a messageProperty, that comes from another source function.

finally to get message when success is false, as it was set as an exception, we have to use opertion.getError()



emp.save({
callback:function(record,operation) {
console.log(operation.wasSuccessful()); //false
console.log(operation.hasException()); //true
// message from server
console.log(operation.getError()); // "record name value is in use"
}
});


hope this helps some1 else.

maneljn
26 Jun 2012, 2:26 AM
great job!!!