Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active February 13, 2019 20:01
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save FokkeZB/5497028 to your computer and use it in GitHub Desktop.
Save FokkeZB/5497028 to your computer and use it in GitHub Desktop.
Alloy constants and helpers for non-Alloy Titanium projects.
var _ = require('underscore')._, // Expects Underscore to be in your Resources folder
Backbone = require('backbone'), // Expects Backbone to be in your Resources folder
platformName = Ti.Platform.name,
OS_ANDROID = (platformName === 'android'),
OS_IOS = (platformName === 'iPhone OS'),
OS_MOBILEWEB = (platformName === 'mobileweb'),
deployType = Ti.App.deployType,
ENV_DEVELOPMENT = (deployType === 'development'),
ENV_TEST = (deployType === 'test'),
ENV_PRODUCTION = (deployType === 'production');
exports._ = _;
exports.Backbone = Backbone;
exports.infect(scope) {
scope.OS_ANDROID = OS_ANDROID;
scope.OS_IOS = OS_IOS;
scope.OS_MOBILEWEB = OS_MOBILEWEB;
scope.ENV_DEVELOPMENT = scope.ENV_DEV = ENV_DEVELOPMENT;
scope.ENV_TEST = ENV_TEST;
scope.EVN_PRODUCTION = scope.ENV_PROD = ENV_PRODUCTION;
}
function isTabletFallback() {
return !(Math.min(
Ti.Platform.displayCaps.platformHeight,
Ti.Platform.displayCaps.platformWidth
) < 700);
}
/**
* @property {Boolean} isTablet
* `true` if the current device is a tablet.
*
*/
exports.isTablet = (function() {
if (OS_IOS) {
return Ti.Platform.osname === 'ipad';
} else if (OS_ANDROID) {
var psc = Ti.Platform.Android.physicalSizeCategory;
return psc === Ti.Platform.Android.PHYSICAL_SIZE_CATEGORY_LARGE ||
psc === Ti.Platform.Android.PHYSICAL_SIZE_CATEGORY_XLARGE;
} else if (OS_MOBILEWEB) {
return !(Math.min(
Ti.Platform.displayCaps.platformHeight,
Ti.Platform.displayCaps.platformWidth
) < 400);
} else {
return isTabletFallback();
}
})();
/**
* @property {Boolean} isHandheld
* `true` if the current device is a handheld device (not a tablet).
*
*/
exports.isHandheld = !exports.isTablet;
/**
* @property {Object} Globals
* An object for storing globally accessible variables and functions.
* Alloy.Globals is accessible in any controller in your app:
*
* Alloy.Globals.someGlobalObject = { key: 'value' };
* Alloy.Globals.someGlobalFunction = function(){};
*
* Alloy.Globals can be accessed in other non-controller Javascript files
* like this:
*
* var theObject = require('alloy').Globals.someGlobalObject;
*
*/
exports.Globals = {};

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.

Alternative all-in-one solution

As an alternative, you could place the next alloy.js file in the Resources folder of your plain Titanium project. Just like the real Alloy, it exposes Underscore, BackBone, formFactor helpers and a Globals property. Now you can use any CommonJS module that (literally) requires Alloy!

By calling infect and passing your current scope (this) all Alloy constants will be set. This method of course is not available in an Alloy environment, so be sure check!

The following code works in both Alloy and plain Titanium environments:

var Alloy = require('alloy');
Alloy.infect && Alloy.infect(this);

// Will work in both Alloy and plain Titanium environments!
if (OS_IOS && Alloy.isTablet) {
        var random = Alloy._.random(0, 100);
}
@sphairo
Copy link

sphairo commented Apr 23, 2014

this is the solution thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment