PDA

View Full Version : Model save not firing success/failure events



billtricarico
3 Jan 2012, 11:33 PM
I have a model that is persisting correctly to the database. Whether it does or does not, the success/failure events do not fire. I am using code similar to the Sencha docs:



obj.save({
success: function(response) {
console.log('obj save success!');
var json = Ext.decode(response.responstText);
},
failure: function(response) {
console.log('obj save failure!');
var json = Ext.decode(response.responstText);
}
});


Does anybody know what I am doing wrong?

skirtle
4 Jan 2012, 12:49 AM
Do you see the requests come back in the Net or Network tab? Do they succeed or fail? What type of proxy are you using? Have you tried adding a callback instead of success/failure? Have you tried putting a breakpoint in the model's save method and stepping through what's going on? Is behaviour consistent across all browsers? Are you using a recent ExtJS version?

It shouldn't stop the console logging but this line will fail:


var json = Ext.decode(response.responstText);

From what I see in the ExtJS source, the first argument passed to the callback is the operation, not the response, and even if it is the response the responstText property is misspelt.

billtricarico
4 Jan 2012, 9:36 PM
I simplified the code just to see if it would help but it made no difference:




model.save({
success: function() {
console.log('success!');
},
failure: function() {
console.log('failure!');
}
});



I am basing this on the docs for the Model: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Model-method-getAssociatedData


I am running Ext 4.0.7.


In the save method, I can see options.success and options.failure, so I know my success and failure events are valid, regardless of their arguments.


I can see that the internal callback responds successfully, but everything halts in the callback function:






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



I surmised that my JSON response from the server was not what Ext was expecting. I changed the JSON to match the model I am saving. However, the result is the same: the record is null and therefore me.set(record.data) is failing with this error:






Uncaught TypeError: Cannot read property 'data' of undefined

skirtle
4 Jan 2012, 10:22 PM
Agreed, it sounds like there's a problem with your response.

I believe the intention of those lines is to allow you to return any updated fields based on the save. For example, a new record might have an id or there might be a field for the last update time.

Unfortunately the way it's been implemented requires you to return your entire record from the server, which has always struck me as a bit odd.

What are you returning from the server? Are you sure it's in the correct format to be read by your model's reader?

billtricarico
4 Jan 2012, 10:27 PM
I changed the JSON again slightly to match what my reader expects and I got it. Thanks for the assist!