Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created March 17, 2015 13:32
Show Gist options
  • Select an option

  • Save aaronksaunders/9ef5af751fbfe8825ee3 to your computer and use it in GitHub Desktop.

Select an option

Save aaronksaunders/9ef5af751fbfe8825ee3 to your computer and use it in GitHub Desktop.
From Simple Appcelerator Titanium Alloy App Videos - https://www.youtube.com/watch?v=Sz_V5HAqroU
//
var peopleService = require('peopleService');
/**
* called when user clicks on table view. The _event will provide
* the index of the item clicked in the table
*
* @param {Object} _event
*/
function clickedOnTableView(_event) {
// display to console log the _event object for debugging
Ti.API.info(JSON.stringify(_event,null,2));
// get data using index provided, show alert
alert("clicked on " + sampleTableData[_event.index].title);
}
// Set the sample data, the tableView by default will render what the
// title property is set to
var sampleTableData = [
{"title":"Aaron", },
{"title":"Andrea"},
{"title":"Bryce"},
{"title":"Reina"}
];
// set data
$.myTableView.data = sampleTableData;
// open the view
$.index.open();
peopleService.getPeople(function(_response){
if (_response.success) {
} else {
Ti.API.info('ERROR - ' + _response.error);
}
});
".container": {
backgroundColor:"white"
}
"Label": {
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: "#000"
}
"#label": {
font: {
fontSize: 12
}
}
<Alloy>
<!-- on IOS we add the NavigationWindow to wrap UI,
this will place the tableView properly in the page -->
<NavigationWindow>
<Window class="container">
<!-- listen for click and call the function specified -->
<TableView id="myTableView" onClick="clickedOnTableView"></TableView>
</Window>
</NavigationWindow>
</Alloy>
exports.getPeople = function(_callback) {
// https://randomuser.me/ for information on API
//http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Network.HTTPClient
// specify the numbers of user you want returned
var url = 'http://api.randomuser.me/?results=20';
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
Ti.API.info("Received text: " + this.responseText);
// API will convert to JSON for you!!
_callback({
success : true,
data : this.responseJSON
});
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e.error);
_callback({
success : false,
error : e
});
},
timeout : 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment