Skip to content

Instantly share code, notes, and snippets.

@DimitarChristoff
Created August 17, 2012 12:30
Show Gist options
  • Save DimitarChristoff/3378472 to your computer and use it in GitHub Desktop.
Save DimitarChristoff/3378472 to your computer and use it in GitHub Desktop.
Request.checkSomething = new Class({
// override Request.JSON and overload results into storage.
Extends: Request.JSON,
Implements: Storage,
options: {
secure: true
},
initialize: function(options) {
this.storageMethod = 'localStorage';
this.setupStorage();
this.parent(options);
},
send: function(options) {
options = options || this.options;
// check if they are cached. if so, use them instead.
this.key = options.key;
var data = this.getItem(this.key);
if (data) {
// compat.
this.onSuccess(data, data);
this.fireEvent("retrieve", this.key, data);
return;
}
this.parent(options);
},
success: function(text, xml){
// override the success so we can store the result into storage.
var json;
try {
json = this.response.json = JSON.decode(text, this.options.secure);
} catch (error){
this.fireEvent('error', [text, error]);
return;
}
if (json == null)
this.onFailure();
else {
// store it.
this.setItem(this.key, json);
this.onSuccess(json, text);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment