PDA

View Full Version : [CLOSED] Model considers the save operation as successfull, not considering the Proxy's succes



interconcept
7 Oct 2011, 12:14 AM
Hello,

we have a made a new Proxy, that extends Ext's Rest Proxy. This proxy expects the success property to be one of "OK", "WARN", "ERROR" and if the value is not "OK", sets the operation's success property to false. The problem is, no matter what the proxy does, the model accepts the response and considers that the model was saved.

An example of a problem:
1. Create a new model and save it. The proxy makes a POST request to save a new model.
2. The server replies with success:"ERROR" ( it is a validation error)
3. Our proxy sets operation's success to false, so that the failure method of the model gets executed (and this happens as desired).
4. On the next save() call, a PUT HTTP request is made, which means UPDATE an existing model. This is the actual BUG... The failure method of the method gets executed, BUT the model is not considered as phantom anymore.

Of course this is not ONE example.

mdlincoln
7 Oct 2011, 7:19 AM
Please provide the source code for a concise test case that demonstrates the issue using the smallest amount of code possible, that does not have any external dependencies, that has all needed data available to be loaded locally (using a json or xml flat file), and that is locally runnable and we will do our best to investigate it.

interconcept
9 Oct 2011, 11:40 PM
//Proxy Definition
Ext.define('my.data.proxy.Rest',
{
extend: 'Ext.data.proxy.Rest',
alias: 'proxy.my.rest',
defaultReaderType: 'obr.json',

/**
* Hooks in the passed callback to execute our functions.
* @param operation
* @param callback
* @param scope
*/
doRequest: function(operation, callback, scope) {

//save the desired callback and scope
this.scope = scope;
this.callback = callback;

callback = function(op) {
if (op.exception) {
} else if (op.wasSuccessful()) {// HTTP status 200
op.success = false;//force the success to always be false.
}

if (typeof this.callback == 'function') {
this.callback.call(this.scope||this, operation );
}
};

this.callParent([operation, callback, this]);

}
}
);

//MODEL Definition
Ext.define('my.model.Segment', {
extend : 'Ext.data.Model',
fields : [ {
name : 'id'
}, {
name : 'name'
}],

proxy : {
type : 'my.rest',
url : "CHANGE_THIS_PATH_TO_THE_RESPONSE_BELOW"
}
});


//Showing the problem
delete myModel;
var myModel = Ext.create("my.model.Segment");
console.log("Before save " + myModel.phantom)
myModel.save({failure:function(){
console.log(this);
console.log("After save " + this.phantom)
this.save();
}});






The response is the next one
{
success: "ERROR"
}



In "Showing the problem" block these happen:
1. A POST HTTP request is made, meaning a new model gets saved.
2. The proxy decides that the reponse is not successful, meaning the next save operation should be again a POST.
3. In the failure function, a new PUT request is made, which means an existing in DB model gets saved.

interconcept
10 Oct 2011, 12:02 AM
Of course it is the Proxy.Server Class that commits the reccords, but it is not correct place to do that, as it is a "private class", as notified in the API Documentation, which cannot be used/changed by us.

evant
10 Oct 2011, 12:09 AM
Can't confirm this. Using 4.0.6:



Ext.define('Thing', {
extend: 'Ext.data.Model',
fields: ['name'],
proxy: {
type: 'ajax',
api: {
create: 'data.json?create=true',
update: 'data.json?update=true'
},
reader: {
type: 'json',
root: 'data'
}
}
})

Ext.onReady(function() {

var foo = new Thing({
name: 'foo'
});

foo.save({
failure: function(){
setTimeout(function(){
foo.save();
}, 100);
}
});

});


Response:


{
"success": false,
"data": [{
"name": "a"
}]
}


Both requests go through the create API.

interconcept
10 Oct 2011, 5:10 AM
If in the response code the success property is not false, than the situation is as I explained.

evant
10 Oct 2011, 6:12 AM
Success is supposed to be a boolean value. If you're sending in another data type you should put it in some other property.

interconcept
11 Oct 2011, 12:08 AM
I see. Please consider the next bug/problem here http://www.sencha.com/forum/showthread.php?150203-Model-save-operation-with-no-response&p=658028#post658028