Skip to content

Instantly share code, notes, and snippets.

@oroce
Created November 24, 2011 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oroce/1392449 to your computer and use it in GitHub Desktop.
Save oroce/1392449 to your computer and use it in GitHub Desktop.
An idea (it means: i haven't tested yet) how to use Backbone.sync with appcelerator (Ti.Database)
var Model = Backbone.Model.extend({
defaults:{
title: "",
content: "",
crDate: new Date(),
isDone: false,
dueDate: null
},
validate: function( attrs ){
var errors = [];
if( !attrs.title || !String.prototype.trim.call( attrs.title ) ){
errors.push({
error: "title is missing",
message: "you shouldn't leave title empty..."
});
}
return errors.length ? errors : undefined;
},
});
var Collection = Backbone.Collection.extend({
model: Model,
store: new Store( "todo.sqlite", "mainDb", "todoTable" )
});
//Later if you a define an external id attribute
//you can sync to a remote server :)
var Store = function( dbFile, dbName, tableName ){
this.dbFile = dbFile;
this.dbName = dbName;
this.tableName = tableName;
this.db = Ti.Database.install( dbFile, dbName );
};
_.extend( Store.prototype, {
//we don't the guid, let the db handle unique keys (auto increment)
create: function( model ){
var columnsList = Object.keys( model.attributes ),
columns = columnsList.join( "," ),
questionMarks = new Array( columnsList.length + 1 ).join( "?," ).splice( 0, -1 );
this.db.execute.apply( this.db,
_.union(
[ String.format( "INSERT INTO %s ( %s ) VALUES( %s )", this.tableName, columns, questionMarks ) ],
Object.values( model.attributes )
)
);
return this.db.lastInsertRowId;
}
update: function( model ) {
var setPart = _.map( Object.keys( model.attributes ), function( item ){ return String.format( "%s = ?", item) }).join( "," );
this.db.execute.apply( this.db
_.union(
[ String.format( "UPDATE %s SET %s WHERE %s = ?", this.tableName, setPart, model.idAttribute ) ],
Object.values( model.attributes ),
model.attributes[ model.idAttribute ]
)
);
return model;
},
// Retrieve a model from `this.data` by id.
find: function( model ) {
var row = this.db.execute( String.format( "SELECT * FROM %s WHERE %s = ?", this.tableName, model.idAttribute ), model.attributes[ model.idAttribute ] );
var ret = {};
if( row && row.isValidRow() ){
_.each( Object.keys( model.attributes ), function( column ){
ret[ column ] = row.fieldByName( column );
});
}
return ret;
},
// Return the array of all models currently in storage.
findAll: function() {
var rows = this.db.execute( String.format( "SELECT * FROM %s", this.tableName, model.idAttribute ) );
var ret = [],
columns = Object.keys( model.attributes ); //cache
while( rows.isValidRow() ){
var _ret = {};
_.each( , function( column ){
_ret[ column ] = rows.fieldByName( column );
});
ret.push( _ret );
rows.next();
}
return ret;
},
destroy: function(model) {
this.db.execute( String.format( "DELETE FROM %s WHERE %s = ?", this.tableName, model.idAttribute ), model.attributes[ model.idAttribute ] );
return model;
}
});
// idea and structure is stolen from: https://github.com/jeromegn/Backbone.localStorage
Backbone.sync = function(method, model, options, error) {
// Backwards compatibility with Backbone <= 0.3.3
if (typeof options == 'function') {
options = {
success: options,
error: error
};
}
var resp,
db = model.db || model.collection.db,
tableName = model.tableName || model.collection.tableName;
switch (method) {
case "read": resp = model.id ? store.find(model) : store.findAll(); break;
case "create": resp = store.create(model); break;
case "update": resp = store.update(model); break;
case "delete": resp = store.destroy(model); break;
}
if (resp) {
options.success(resp);
} else {
options.error("Record not found");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment