public
Created

  • Download Gist
BackboneJSFetch.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12
function (options) {
options = options ? _.clone(options) : {};
if (options.parse === undefined) options.parse = true;
var collection = this;
var success = options.success;
options.success = function(resp, status, xhr) {
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
if (success) success(collection, resp);
};
options.error = Backbone.wrapError(options.error, collection, options);
return (this.sync || Backbone.sync).call(this, 'read', this, options);
}
CollectionDefaults.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
sync: function(method, object, options) {
console.log(method);
options = (options) ? options : {};
 
var resp;
var localStore = this.dualStorage.localStorage;
 
var pickCallback = function(record) {
var logArgs = function(args) { console.log(args); };
options.success = (options.success) ? options.success : logArgs;
options.error = (options.error) ? options.error : logArgs;
 
if (record && record !== []) {
options.success(record);
} else {
options.error({ message: "Could not find record", record: record });
}
};
 
switch (method) {
case "readall": localStore.findAll(pickCallback); break;
 
case "read":
localStore.find(object, pickCallback);
break;
 
case "create": localStore.save(object, pickCallback); break;
 
case "update": localStore.save(object, pickCallback); break;
 
case "updateitem": localStore.saveItem(object, pickCallback); break;
 
case "delete": localStore.destroy(object, pickCallback); break;
 
case "deleteAll": localStore.destroyAll(object, pickCallback); break;
}
}
LocalStorage.Find.js
JavaScript
1 2 3 4 5 6
find: function(truthFunc, callback) {
this.getData(function(records) {
var items = _.filter(records, truthFunc);
callback(items);
});
},

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.