How to use callback functions in Appcelerator’s Titanium

Interested in a hands-on tutorial in Titanium? Building Cross-Platform Apps using Titanium, Alloy, and Appcelerator Cloud Services

As part of our internal “Crash Course To Titanium”, I was teaching our guys how to use a Common JS module to pull data from a remote server and generate data for a tableview.  The problem came when we’d finished generating the data, we had to pass it back to the View.  The hacky solution would be to store the TableView object in a Global variable that can be accessed anywhere.  Of course, that’s not the best solution, a much better one is to use a callback function passed as a parameter to the CommonJS function which is then called when the tableviewdata has been prepared.

Here’s a simplified version of how we got it working: https://gist.github.com/philsmithsonuk/6afe59833daeef8f3f24

Thanks to Ricardo for pointing us in the right direction: https://plus.google.com/u/0/100369688595445841250

Anyone interested in Titanium development should check out our tutorials http://blog.foolprooflabs.com/category/titanium-tutorials/ and also join https://plus.google.com/u/0/communities/103721188616163608038

 



About Phil Smithson

Ultrarunner & CTO. Passions include Mobile app development, Scrum, Kanban, Wordpress and Symfony.

Leave a Reply

Your email address will not be published. Required fields are marked *


*