Skip to content

Instantly share code, notes, and snippets.

@pec1985
Created May 21, 2012 17:46
Show Gist options
  • Save pec1985/2763523 to your computer and use it in GitHub Desktop.
Save pec1985/2763523 to your computer and use it in GitHub Desktop.
Callback examples
//------------ ui.js -------------
/**
* AlertDialog component with callback function
* @param { Object } _params Ti.UI.AlertDialog properties
* and optional "callback"
*/
exports.Alert = function(_params){
_params = _params || {};
var callback = _params.callback;
if(callback != null){
delete _params.callback;
}
var a = Ti.UI.createAlertDialog(_params);
if(callback){
a.addEventListener('click', callback)
}
return a;
}
// ----------- some other file.js ------------
/** how to use */
var UI = require('ui');
var warning = UI.Alert({
title: 'Warning!',
message: 'This is a cool alert message',
buttonNames: ['Ok', 'Sure'],
callback: function(_event){
// do something with the event
// switch (_event.index){
// case 0: ....
// break ......
// }
}
});
// ----------- some file.js ------------
/**
* Example of UI element with callback
* @param { Object } _params Ti.UI.TableView properties
* @param { Function } callback fires when clicked on table
*/
function CustomTable(_params, callback){
var table = Ti.UI.createTableView(_params);
// use this:
if(callback){
table.addEventListener('click', callback);
}
// instead of this:
/**
table.addEventListener('click', function(_event){
Ti.App.fireEvent('table_clicked', _event)
});
*/
return table;
}
/** how to use */
var table = CustomTable({
data: [
{title: 'Hello from row 1'},
{title: 'Hello from row 2'}
]
}, function(_event){
alert(_event.rowData);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment