Appcelerator-Titanium-DB-Model
Appcelerator-Titanium-DB-Model copied to clipboard
This is a simple JS template for a DB model, it's really easy to use and provides some great base functions...
Simple JS Model for Appcelerator Titanium
http://github.com/swanify/Appcelerator-Titanium-DB-Model
This is a really simple template for a model to handle database interaction when using Appcelerator Titanium to build iPhone / Android applications, there are some really useful functions, and you can easily add your own custom functions.
We like to place them in their own folder called models i.e. /Resources/models/item.js
INSTRUCTIONS:
Replace all occurrences of Singular with your desired name, i.e. Item, Person, Setting etc
Replace all occurrences of TABLE with the name of your table, i.e. ITEMS, PEOPLE, SETTINGS
Replace all occurrences of DB with the name of your DB.
Enter all your columns in the this.columns array, created and updated will be automatically updated so you just need to include them in your table.
USAGE:
TO INCLUDE THE MODEL Ti.include('models/item.js');
TO CREATE A NEW ENTRY
var item = new Item(); item.name = 'Example'; item.price = '2.99'; item.save();
TO UPDATE AN ENTRY
var item = new Item(); item.getById(1); item.name = 'Example 2'; item.save();
TO GET NUMBER OF ROWS
var item = new Item(); var numRows = item.countAll();
TO LOOP THROUGH RESULTS
var item = new Item(); var rows = item.getAll(); for ( var i=0, len=rows.length; i<len; ++i ){ Ti.API.info('ID: ' + rows[i].id); Ti.API.info('Name: ' + rows[i].name); }