PDA

View Full Version : Model Load parameters



martinrame
3 Jun 2012, 11:08 AM
I would like to load an instance of a model from an Ajax call, but I can't set my desired parameters.

I know I can do this:


var Test = Ext.ModelManager.getModel('MyApp.model.Test');
Test.load(123, {
success: function(test){
console.log("success");
},
failure: function(){
console.log("failure");
}
});


But using this, the first parameter of Model.load is always "id", how can I change it to another name, such as "itemId"?.

vietits
3 Jun 2012, 3:48 PM
With Ext.data.Model.load(), the first argument is always id of model to load. If you want to pass other parameters, try to use params config in the config argument, which is the second argument of load() method.


var Test = Ext.ModelManager.getModel('MyApp.model.Test');
Test.load(null, {
params: {
id: 123,
itemId: <ITEMID>,
// other params
},
success: function(test){
console.log("success");
},
failure: function(){
console.log("failure");
}
});

martinrame
4 Jun 2012, 3:57 AM
Thanks Vietis, that was exactly what I was looking for,

martinrame
4 Jun 2012, 5:42 PM
A quick question. How should I define the returning JSON in case of failure?

vietits
4 Jun 2012, 6:07 PM
Not sure to understand your question. What is the purpose of returning JSON data when the request is failed?

martinrame
4 Jun 2012, 6:24 PM
One example could be that the requested model is no longer available on server side, in this case, an "Record not found" can be the error message.

vietits
4 Jun 2012, 7:18 PM
Ah, I see. To do that, you should do the following:
1. Config proxy reader with 'messageProperty' set to something such as 'message'.


...
proxy: {
type: 'ajax',
url: 'request url',
reader: {
type: 'json',
root: 'data',
messageProperty: 'message'
}
},
...

2. Define failure handler for the request and get the error message returned from server from operation argument as below:


var Test = Ext.ModelManager.getModel('MyApp.model.Test');
Test.load(null, {
params: {
id: 123,
itemId: <ITEMID>,
// other params
},
success: function(test){
console.log("success");
},
failure: function(record, operation){
console.log(operation.error);
}
});

3. Return json data from server in the form below:


{
"success": false,
"message": "Record not found" // the key must be the same as messageProperty in reader config
}