Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created January 5, 2013 19:17
Show Gist options
  • Save aaronksaunders/4463173 to your computer and use it in GitHub Desktop.
Save aaronksaunders/4463173 to your computer and use it in GitHub Desktop.
Working with model ids when creating sync adapters in BackboneJS | Appcelerator Alloy. This is not just specific to the sql adapter, but should be considered when creating your own custom adapters
function Sync(model, method, opts) {
var table = model.config.adapter.collection_name, columns = model.config.columns, resp = null;
switch (method) {
case "create":
// properly set the model id on object
// and on the model.attributes notice use of 'idAttribute'
// property to account for something other than 'id' being
// used to store the actual attribute
if (!model.id) {
model.id = guid();
model.set(model.idAttribute, model.id);
}
var names = [], values = [], q = [];
for (var k in columns) {
names.push(k);
values.push(model.get(k));
q.push("?");
}
sql = "INSERT INTO " + table + " (" + names.join(",") + ",id) VALUES (" + q.join(",") + ",?)";
values.push(model.id);
db.execute(sql, values);
resp = model.toJSON();
break;
case "read":
debugger;
var sql = "SELECT * FROM " + table, rs = db.execute(sql), len = 0, values = [];
while (rs.isValidRow()) {
var o = {}, fc = 0;
fc = _.isFunction(rs.fieldCount) ? rs.fieldCount() : rs.fieldCount;
_.times(fc, function(c) {
var fn = rs.fieldName(c);
o[fn] = rs.fieldByName(fn);
});
values.push(o);
len++;
rs.next();
}
rs.close();
model.length = len;
len === 1 ? resp = values[0] : resp = values;
break;
case "update":
var names = [], values = [], q = [];
for (var k in columns) {
names.push(k + "=?");
values.push(model.get(k));
q.push("?");
}
var sql = "UPDATE " + table + " SET " + names.join(",") + " WHERE id=?", e = sql + "," + values.join(",") + "," + model.id;
values.push(model.id);
db.execute(sql, values);
resp = model.toJSON();
break;
case "delete":
var sql = "DELETE FROM " + table + " WHERE id=?";
db.execute(sql, model.id);
model.id = null;
resp = model.toJSON();
}
if (resp) {
opts.success(resp);
method === "read" && model.trigger("fetch");
} else
opts.error("Record not found");
}
//
// THIS IS THE INCORRECT IMPLEMENTATION
// SEE COMMENTS BELOW
//
function Sync(model, method, opts) {
var table = model.config.adapter.collection_name, columns = model.config.columns, resp = null;
switch (method) {
case "create":
var names = [], values = [], q = [];
for (var k in columns) {
names.push(k);
values.push(model.get(k));
q.push("?");
}
// QUESTIONABLE: should check that there is not id set at this time
var id = guid(), sql = "INSERT INTO " + table + " (" + names.join(",") + ",id) VALUES (" + q.join(",") + ",?)";
values.push(id);
db.execute(sql, values);
//
// HAS TWO ISSUES: See backbone documentation for additional information
// 1) does not account for the use of the 'idAttribute' property on the model
// 2) NEVER sets the actual model's attribute for the id
//
model.id = id; // <-- WRONG
resp = model.toJSON();
break;
case "read":
var sql = "SELECT * FROM " + table, rs = db.execute(sql), len = 0, values = [];
while (rs.isValidRow()) {
var o = {}, fc = 0;
fc = _.isFunction(rs.fieldCount) ? rs.fieldCount() : rs.fieldCount;
_.times(fc, function(c) {
var fn = rs.fieldName(c);
o[fn] = rs.fieldByName(fn);
});
values.push(o);
len++;
rs.next();
}
rs.close();
model.length = len;
len === 1 ? resp = values[0] : resp = values;
break;
case "update":
var names = [], values = [], q = [];
for (var k in columns) {
names.push(k + "=?");
values.push(model.get(k));
q.push("?");
}
var sql = "UPDATE " + table + " SET " + names.join(",") + " WHERE id=?", e = sql + "," + values.join(",") + "," + model.id;
values.push(model.id);
db.execute(sql, values);
resp = model.toJSON();
break;
case "delete":
var sql = "DELETE FROM " + table + " WHERE id=?";
db.execute(sql, model.id);
model.id = null;
resp = model.toJSON();
}
if (resp) {
opts.success(resp);
method === "read" && model.trigger("fetch");
} else
opts.error("Record not found");
}
// Sample Code from Alloy User Group Question
// https://groups.google.com/forum/#!topic/appc-ti-alloy/p4GCGIOKvhk
//
var people = Alloy.createCollection('person');
console.log('creating person');
var newPerson = people.create({
name : 'wuff',
age : parseInt(Math.random() * 99)
});
console.log('created person ' + JSON.stringify(newPerson) + ' with id: ' + newPerson.id);
console.log('getting person with id: ' + newPerson.id);
var person = people.get(newPerson.id);
console.log('got person ' + JSON.stringify(person));
person.destroy();
console.log('destroyed');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment