PDA

View Full Version : Scripttag Proxy: Handinling Request Failure



matt_d_rat
1 Jul 2011, 8:10 AM
How should I properly format my failure response and what callback function should I call to handle the failure?

I am returning data by using the scripttag proxy from our API for a list of people based on the following model:

Entity Model:


Ext.regModel("Entity", {
fields: [
{name: "entity_id", type: "int"},
{name: "company_name", type: "string"},
{name: "building", type: "string"},
{name: "address1", type: "string"},
{name: "address2", type: "string"},
{name: "town", type: "string"},
{name: "county", type: "string"},
{name: "postcode", type: "string"},
{name: "phone1", type: "string"},
{name: "fax", type: "string"},
{name: "website", type: "string"},
{name: "email", type: "string"},
],
proxy: {
type: 'localstorage',
id: 'entity'
}
});


Entity Store:


Ext.regStore("Entity", {
model: 'Entity',
storeId: 'entityStore',
sorters: 'company_name',
getGroupString : function(record) {
return record.get('company_name')[0];
},
proxy: {
type: 'scripttag',
url: /path/to/api/url/
callbackPrefix: 'listEntities',
callbackParam: 'callback',
extraParams: {
apiKey: 'demo',
method: 'listEntities',
output: 'json'
},
reader: {
type: 'json',
}
}
});


Our API requires you to be authenticated (which is done at login via another store) so that it can create a session on the server to handle your future calls via the API key you provide.

However this is time sensitive and the session expires after a certain number of hours, so if the user happens to leave the app running on their device and come back to it later and make another API call but they are no longer authenticated the following error is thrown (presumably because of the sorter not receiving the right data):

Error thrown when calling API and not having an authenticated session:


Uncaught TypeError: Cannot call method 'toLowerCase' of undefined


Response returned by the server:


listEntities1003("Login failed");


I am not autoLoading the store, only loading when the view is loaded via:

Callback handle on store load:


this.loadMask.show();

this.list.store.load({

callback: function (records, operation, success) {

this.loadMask.hide();
},
scope: this
});