PDA

View Full Version : AJAX Exception Handling



bweiler
13 Dec 2011, 9:14 AM
I've tried several approaches to trap ajax successes and failures, but I can't seem to get it working. What is the correct approach to use to trap 500 errors and json data format errors? I'm not sure it matters, but I'm using Ext.define and not Ext.create and I'm using PR3.

Thanks,

Bruce




Ext.define('MyModel', {
extend: 'Ext.data.Model',

fields: [
{name: "field", type: "string"},
],

proxy: {
type: 'ajax',
url: "users.json",
reader: 'json',

listeners: {
exception: {
success: function() {
console.log("MyModel success.");
},
failure: function() {
console.log("MyModel failure.");
}
},
success: function(response) {
console.log("MyModel: success", response);
},
failure: function(response) {
console.log("MyModel: failure", response);
}
},
exception: function(resp, oper, opts) {
console.log("MyModel: exception", resp, oper, opts);
},
success: function(response) {
console.log("MyModel: success", response);
},
failure: function(response) {
console.log("MyModel: failure", response);
}
}

mitchellsimoens
13 Dec 2011, 9:30 AM
How are you loading the data? From a Store or from Model.load?

bweiler
13 Dec 2011, 10:24 AM
I'm loading the data from the store using autoLoad: true.

mitchellsimoens
13 Dec 2011, 10:32 AM
I'm loading the data from the store using autoLoad: true.

Ok... so listen for the exception event on the store.

bweiler
13 Dec 2011, 11:34 AM
I tried the following and it still doesn't work. If the code is correct below, then I'll try to come up with a working example that shows the problem.

Thanks,

Bruce



Ext.define('MyData', {
extend: 'Ext.data.Store',
model: 'MyModel',
autoLoad: true,
exception: function(resp, oper, opts) {
console.log("MyModel: exception", resp, oper, opts);
},
}

mitchellsimoens
13 Dec 2011, 11:38 AM
I tried the following and it still doesn't work. If the code is correct below, then I'll try to come up with a working example that shows the problem.

Thanks,

Bruce



Ext.define('MyData', {
extend: 'Ext.data.Store',
model: 'MyModel',
autoLoad: true,
exception: function(resp, oper, opts) {
console.log("MyModel: exception", resp, oper, opts);
},
}



Of course that won't work. You created a new method, not listener...


Ext.define('MyData', {
extend : 'Ext.data.Store',

model : 'MyModel',
autoLoad : true,

constructor: function(config) {
this.callParent(arguments);

this.on('exception', someFn, this);
}
});

bweiler
13 Dec 2011, 12:51 PM
I'm very excited about Sencha Touch 2.0. The MVC approach is much cleaner and easy to understand and I am able to implement application features twice as fast as with 1.x. I hope the ajax exception handling will also be improved by the time 2.0 releases.

I'm not sure if the Ext.data.StoreManager listeners configuration option is an artifact or an unimplemented feature, but it would be nice if it worked.

In the meantime, I can see from your code how to set a listener on the store from within the MVC controller using the Ext.data.StoreManager.get() method if I don't use autoLoad. Otherwise, it looks like I will need to use the internal approach you described above when using the autoLoad option in the stores.

Thanks again for your help,

Bruce