O'Reilly logo

Appcelerator Titanium: Up and Running by John Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Customizing Titanium

The previous chapters have given you an overview of Titanium’s language, namespaces, objects, and methods, hopefully offering you plenty of neat stuff to work with. But Titanium’s foundation in JavaScript also makes it easy to extend and customize. JavaScript lets you create your own prototype functions that add functionality to any class you want. For instance, you can easily create a string prototype function that adds functionality to any string object created in JavaScript, like this:

String.prototype.trim = function() { 
    return this.replace(/^\s+|\s+$/, ''); };

This line of code adds a trim function to any JavaScript string object you create. Prototypes are a very cool feature of JavaScript and should be exploited profusely. They allow you to add functions that perhaps you’ve gotten used to in other languages and would like to take advantage of while working with Titanium.

You can also make “mini-factories,” which are basically functions that create Titanium objects with certain properties or other traits that you’ll need regularly. Creating these mini-factories will allow you to have a central point where you can make changes, and all the calls to these factories will return an object with those changes in it.

What’s in a Name...Space

Titanium contains many separate objects, making them available via namespaces. In general terms, a namespace is an abstract container that provides a context for the items that it contains.

The topmost object in the Titanium ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required