├── README └── models └── template.js /README: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------- 2 | Simple JS Model for Appcelerator Titanium 3 | ---------------------------------------------------------------- 4 | 5 | http://github.com/swanify/Appcelerator-Titanium-DB-Model 6 | 7 | This is a really simple template for a model to handle database 8 | interaction when using Appcelerator Titanium to build iPhone / 9 | Android applications, there are some really useful functions, 10 | and you can easily add your own custom functions. 11 | 12 | We like to place them in their own folder called models i.e. 13 | /Resources/models/item.js 14 | 15 | ---------------------------------------------------------------- 16 | INSTRUCTIONS: 17 | ---------------------------------------------------------------- 18 | Replace all occurrences of Singular with your desired name, i.e. 19 | Item, Person, Setting etc 20 | 21 | Replace all occurrences of TABLE with the name of your table, 22 | i.e. ITEMS, PEOPLE, SETTINGS 23 | 24 | Replace all occurrences of DB with the name of your DB. 25 | 26 | Enter all your columns in the this.columns array, created and 27 | updated will be automatically updated so you just need to 28 | include them in your table. 29 | 30 | ---------------------------------------------------------------- 31 | USAGE: 32 | ---------------------------------------------------------------- 33 | TO INCLUDE THE MODEL 34 | Ti.include('models/item.js'); 35 | 36 | ---------------------------------------------------------------- 37 | TO CREATE A NEW ENTRY 38 | ---------------------------------------------------------------- 39 | var item = new Item(); 40 | item.name = 'Example'; 41 | item.price = '2.99'; 42 | item.save(); 43 | 44 | ---------------------------------------------------------------- 45 | TO UPDATE AN ENTRY 46 | ---------------------------------------------------------------- 47 | var item = new Item(); 48 | item.getById(1); 49 | item.name = 'Example 2'; 50 | item.save(); 51 | 52 | ---------------------------------------------------------------- 53 | TO GET NUMBER OF ROWS 54 | ---------------------------------------------------------------- 55 | var item = new Item(); 56 | var numRows = item.countAll(); 57 | 58 | ---------------------------------------------------------------- 59 | TO LOOP THROUGH RESULTS 60 | ---------------------------------------------------------------- 61 | var item = new Item(); 62 | var rows = item.getAll(); 63 | for ( var i=0, len=rows.length; i 0){ 76 | while (result.isValidRow()) 77 | { 78 | eval('var thisRow = new ' + this.className + '();'); 79 | // Set all of this classes variables to the data 80 | for ( var i=0, len=this.columns.length; i