Sencha Touch Adding Records to a Store From a Client Side SQL Lite DB

Posted by Bhushan G Ahire | Posted in Featured | Posted on 05-05-2011

0

// Declare variable to use as data when instantiating store
var favorites = [];

// Set up datamodel
Ext.regModel('Codes', {
    fields: [
        {name: 'code',      type: 'string'},
        {name: 'desc',		type: 'string'}
    ]
});

var favorites_store = new Ext.data.Store({
	model  : 'Codes',
	data: favorites
});

// Get the data from the database and call the function allDataSelectHandler when finished
function getFavorites(){
  CodesDB.transaction(
      function (transaction) {
          transaction.executeSql('SELECT * FROM favorites;', [],  allDataSelectHandler);
      }
  );
}

function allDataSelectHandler(transaction, results){
 if (results.rows.length == 0){

} else {
// Loop through the records and add them to the store
    for (var i=0; i < results.rows.length; i++){
          row = results.rows.item(i);
	        favorites_store.add({'code':row['code'],'desc':row['desc']});
				//alert(row['code']);
     }
  }
}
initDatabase();
getFavorites();