Skip to content

Instantly share code, notes, and snippets.

@coderberry
Created March 14, 2012 04:01
Show Gist options
  • Select an option

  • Save coderberry/2033966 to your computer and use it in GitHub Desktop.

Select an option

Save coderberry/2033966 to your computer and use it in GitHub Desktop.
backbone-websql.js
// ====== [UTILS] ======
//function for generating "random" id of objects in DB
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
// ====== [ WebSQLStore ] ======
var WebSQLStore = function (initSuccessCallback, initErrorCallback) {
var success, error;
this.db = window.openDatabase('wwpro', '1.0', 'Weight Watchers Pro', 1024 * 1024 * 3);
success = function (tx, res) {
if (initSuccessCallback) initSuccessCallback();
};
error = function (tx, error) {
console.log("Error while create table", error);
if (initErrorCallback) initErrorCallback();
};
this.db.transaction (function(tx) {
var sql = "CREATE TABLE IF NOT EXISTS tracker_items (id INTEGER PRIMARY KEY, name TEXT, points TEXT, date TEXT)";
console.log("Executing: " + sql);
tx.executeSql(sql, [], success, error);
});
};
_.extend(WebSQLStore.prototype, {
create: function (model, success, error) {
console.log("sql create");
if (model.attributes.apiid) {
model.id = model.attributes.id = model.attributes.apiid;
} else {
model.id = model.attributes.id = guid();
}
var sql = "INSERT INTO tracker_items (name, points, date) VALUES (?, ?, ?)";
var values = [model.get('name'), model.get('points'), model.get('date')];
this._executeSql(sql, values, success, error);
},
destroy: function (model, success, error) {
console.log("sql destroy");
var id = (model.id || model.attributes.id);
this._executeSql("DELETE FROM tracker_items WHERE id = ?", [model.id], success, error);
},
find: function (model, success, error) {
console.log("sql find");
var id = (model.id || model.attributes.id);
var sql = "SELECT id, name, points, date FROM tracker_items WHERE id = ?";
this._executeSql(sql, [id], success, error);
},
findAll: function (model, success, error) {
console.log("sql findAll");
this._executeSql("SELECT id, name, points, date FROM tracker_items", [], success, error);
},
update: function (model, success, error) {
console.log("sql update")
var id = (model.id || model.attributes.id);
var sql = "UPDATE tracker_items SET name = ?, points = ?, date = ? WHERE id = ?"
var values = [model.get('name'), model.get('points'), model.get('date'), id];
this._executeSql(sql, values, success, error);
},
_save: function (model, success, error) {
console.log("sql _save");
var id = (model.id || model.attributes.id);
this.db.transaction(function(tx) {
tx.executeSql("");
});
},
_executeSql: function (SQL, params, successCallback, errorCallback) {
var success = function(tx, result) {
console.log(SQL + " - finished");
if (successCallback) successCallback(tx, result);
};
var error = function(tx, error) {
console.log(SQL + " - error: " + error);
if (errorCallback) errorCallback(tx, error);
};
this.db.transaction(function(tx) {
tx.executeSql(SQL, params, success, error);
});
}
});
// ====== [ Backbone.sync WebSQL implementation ] ======
Backbone.sync = function (method, model, options) {
var store = model.store || model.collection.store, success, error;
if (store == null) {
console.warn("[BACKBONE-WEBSQL] model without store object -> ", model);
return;
}
success = function (tx, res) {
var len = res.rows.length, result, i;
if (len > 0) {
result = [];
for (i=0; i<len; i++) {
console.log(res.rows.item(i));
result.push(res.rows.item(i));
}
}
options.success(result);
};
error = function (tx,error) {
console.error("sql error");
console.error(error);
console.error(tx);
options.error(error);
};
switch(method) {
case "read": (model.id ? store.find(model, success, error) : store.findAll(model, success, error));
break;
case "create": store.create(model, success, error);
break;
case "update": store.update(model, success, error);
break;
case "delete": store.destroy(model, success, error);
break;
default:
console.log(method);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment