Skip to content

Instantly share code, notes, and snippets.

@artworker
artworker / ALLOY.md
Last active August 29, 2015 14:16 — forked from FokkeZB/ALLOY.md

If you want to your CommonJS modules to work in both Alloy and plain Titanium projects, you might need a way to detect if you're in Alloy. For instance, if you're in Alloy you would get Underscore from the alloy-module, while in plain Titanium you would require Underscore directly.

Well, you can:

var _ = require((typeof ENV_TEST === 'boolean') ? 'alloy' : 'underscore')._;

The above works by utilizing Alloy's optimization process. In this process, constants like ENV_TEST will be either TRUE or FALSE. The actual expressions in wich they are used will then be evaluated. If FALSE the code block will be removed. In plain Titanium projects the constants are undefined and this typeof ENV_TEST will be undefined, so the code block will be executed.

@artworker
artworker / ALLOY.md
Last active August 29, 2015 14:12 — forked from FokkeZB/ALLOY.md

If you want to your CommonJS modules to work in both Alloy and plain Titanium projects, you might need a way to detect if you're in Alloy. For instance, if you're in Alloy you would get Underscore from the alloy-module, while in plain Titanium you would require Underscore directly.

Well, you can:

var _ = require((typeof ENV_TEST === 'boolean') ? 'alloy' : 'underscore')._;

The above works by utilizing Alloy's optimization process. In this process, constants like ENV_TEST will be either TRUE or FALSE. The actual expressions in wich they are used will then be evaluated. If FALSE the code block will be removed. In plain Titanium projects the constants are undefined and this typeof ENV_TEST will be undefined, so the code block will be executed.

// Works on 3.2.0GA
var win = Ti.UI.createWindow({
backgroundColor : '#ffffff',
exitOnClose : true // this makes it a HEAVYWEIGHT window
});
var myView = Ti.UI.createView();
var curBtn = Ti.UI.createButton({
color : '#000000',