Thursday, 18 July 2013

Alloy - RSS Feed Parser

RSS feeds benefit publishers by letting them syndicate content automatically. A standardized XML file format allows the information to be published once and viewed by many different programs. They benefit readers who want to subscribe to timely updates from favorite websites or to aggregate feeds from many sites into one place.

I am creating a simple RSS Reader which get the rss feed from given url and parse the xml data.

And return array of objects which is easy to manipulate in your titanium application.

I am creating sample project in alloy framework.

Download source code from github RSS-Feed-Reader

You can find rss.js  file in /lib directory.

usage of this file is,

var rss = require('rss');
var RSS_URL = OS_MOBILEWEB ? '/goirss.xml' : '';
// open detail window
function openDetail(e) {
 $.trigger('detail', e);

// Refresh table data from remote RSS feed
function refreshRss() {
 rss.loadRssFeed(RSS_URL, {
  success : function(data) {
   var rows = [];
   _.each(data, function(item) {
    rows.push(Alloy.createController('row', {
     articleUrl :,
     title : item.title,
     date : item.pubDate,
     description : item.description

// do initial load of RSS

Thank you.

Friday, 12 July 2013

Alloy - Sqlite CreateTable Example

I created the sample project for createTable into preloaded sqlite database in Alloy Framework

download working example form gist alloySqliteCreateTable

Main logic written in app/controller/index.js

function createTable(e) {  
 var db ='codeguru');
  db.execute('CREATE TABLE people (name TEXT, phone_number TEXT, city TEXT)');
  var thisName = 'Arthur';
  var thisPhoneNo = '1-617-000-0000';
  var thisCity = 'Mountain View';
  db.execute('INSERT INTO people (name, phone_number, city) VALUES (?, ?, ?)', thisName, thisPhoneNo, thisCity);

  var personArray = ['Paul','020 7000 0000', 'London'];
  db.execute('INSERT INTO people (name, phone_number, city) VALUES (?, ?, ?)', personArray);

  var rows = db.execute('SELECT rowid,name,phone_number,city FROM people');
  while (rows.isValidRow())
  {'Person ---> ROWID: ' + rows.fieldByName('rowid') + ', name:' + rows.field(1) + ', phone_number: ' + rows.fieldByName('phone_number') + ', city: ' + rows.field(3));;

This is an quick fix for creating table in runtime. And I will working on collection of data inserted through model.js 

 For now, Refer this following repository for bulk updates in sqlite db alloy framework, One way to do bulk updates and deletes with Appcelerator Alloy Collections bulk_update

Thursday, 11 July 2013

JSON object Sorted by Date


Many of them asking how to sort my json objects based on date. And writing many lines of code, sometimes get tired to sort the json object. Here I found simple function to sort your json object based on date.

Not only date you can sort json object based on any attribute by writing custom sort function in your code.

Let me explain how to create custom sort function in java script.

You can download the sample app source code from git repository Sort Json by Date

Example JSON object:
var latestOffers=[
          {id: '1', date: '20 Dec 2012, 8:00 am', text:'item1'},
          {id: '2',  date: '29 Jun 2012, 5:47 pm', text:'item2'},
          {id: '3', date: '15 May 2013, 6:40 pm', text:'item3'}         

Now I write sortbydate() function

function sortbydate(a, b) {
    // change the < condition to > for reverse the sort.
    // here you can add your own sorting condition 
    // eg,   return <;   //its sort your object based on id.
    return new Date( < new Date(;

Now you can call the Array.sort() with your sortbydate function.

latestOffers.sort(sortbydate);'sorted json array ==='+ JSON.stringify(latestOffers));


Alloy Framework - Use Preloaded Sqlite Database

Most of time you may need to use the preloaded db in your application.

We are going to see How to use preloaded SQLite Database in Alloy framework in titanium.

Download the source code from git repository  AlloySqlitedb

1. Open the Titanium Studio  (I am using 3.1.1)
2. create New Alloy Mobile Project  (refer the Link for creating New Alloy Project)
3. Put your sqlite database file into app/assets folder. (I am using myapp.sqlite)
4.create collections object in alloy.js file.

Alloy.Collections.fighters = Alloy.createCollection('fighters');

5.create model file "fighters.js" in app/model directory.

exports.definition = {
    "db_file": "/myapp.sqlite",
    "db_name": "fighters",
    "idAttribute": "id",

6.Modify index.xml file in app/views/ directory - create Table View.



7. Create controller index.js file in app/controller directory

var fighters = Alloy.Collections.fighters;
var counter = 1;

function showId(e) {
 if (e.row.model) {
  var detailObj=fighters.get(e.row.model);
  var win=Alloy.createController('detail',{"$model":detailObj});

function addTestFighter(e) {
 // create the test fighter model
 var model = Alloy.createModel('fighters', {
  name: 'Name ' + counter,
  nickname: 'Nickname ' + counter

 // add model to the collection and save it to sqlite

 // let's refresh so we can see the ids coming from the 
 // autoincrement field in the sqlite database in the 
 // row click alerts



8. Create layout for row.xml in app/views directory



9. Create controller row.js in app/controller directory

// Attach the bound model ($model) to the row so 
// we can access it in a click event.
if ($model) {
 $.row.model = $model.toJSON();

10. Create layout for for detail.xml in app/views directory



11. Create controller detail.js in app/controller directory.

var args=arguments[0]||{};
if(args.$model){'model ='+JSON.stringify(args.$model));
 var dataJson=args.$model.toJSON();
 alert('data not passed');

12. Finally add the styles to your layouts in app/styles

a. index.tss

"Window": {
 backgroundColor: '#fff',
 navBarHidden: true,
 exitOnClose: true

"#title": {
 top: 0,
 height: '50dp',
 width: Ti.UI.FILL,
 color: '#fff',
 backgroundColor: '#a00',
 font: {
  fontSize: '36dp',
  fontWeight: 'bold'
 textAlign: 'center'

"#table": {
 top: '50dp',
 bottom: 0


"#row": {
 height: '60dp',
 backgroundColor: '#fff'

"Label": {
 height: Ti.UI.SIZE,
 width: Ti.UI.SIZE

"#name": {
 top: '7dp',
 left: '7dp',
 font: {
  fontSize: '24dp',
  fontWeight: 'bold'

"#nickname": {
 bottom: '7dp',
 left: '14dp',
 font: {
  fontSize: '16dp',
  fonWeight: 'normal'

c. detail.tss

"Window": {
 backgroundColor: '#fff',
 navBarHidden: true
"#name": {
 top: '10dp',
 font: {
  fontSize: '24dp',
  fontWeight: 'bold'

"#nickname": {
 top: '10dp',
 font: {
  fontSize: '16dp',
  fonWeight: 'normal'

13. Here we go and run the project.


Alloy Animation - builtin library

Titanium alloy framework provide collection of useful animation utilities. To use the animation builtin library, all you need to do is require it with the alloy root directory in your require call. For example:

var animation = require('alloy/animation');
animation.crossFade(view1, view2, 500, finishCallback);

chainAnimate( Titanium.UI.View view, Titanium.UI.Animation[] animations, [function() finishCallback] )
Executes a series of animations on the target view.
crossFade( Titanium.UI.View from, Titanium.UI.View to, Number duration, [function() finishCallback] )
Transitions from one view to another using a cross-fade animation.
fadeAndRemove( Titanium.UI.View from, Number duration, Titanium.UI.View container, [function() finishCallback] )
Fades out a view then removes it from its parent view. 
fadeIn( Titanium.UI.View to, Number duration, [function() finishCallback] )
Fades in the specified view. 
fadeOut( Titanium.UI.View to, Number duration, [function() finishCallback] )
Fades out the specified view. 
flash( Titanium.UI.View view, [Number delay], [function() finishCallback] )
Flashes the target view twice, fading to partially transparent then back to fully-opaque. 
popIn( Titanium.UI.View view, [function() finishCallback] )
Makes the specified view appear using a "pop-in" animation, which combines a fade-in with a slight expanding and contracting animation, to call attention to the new view. 
shake( Titanium.UI.View view, [Number delay], [function() finishCallback] )
Creates a shake animation, moving the target view back and forth rapidly several times.


1. open Titanium Studio
3. Modify views/index.xml

4. Modify styles/index.tss
".container": {
"#square": {
 width: 200,
 height: 200,
 color: "#f00"

 title:'Shake Me',

5. Modify controllers/index.js
function shakeMe(e) {
    var animation = require('alloy/animation');
    animation.shake($.view2, 500);


6. Run the project and click the button.