Skip to content

Instantly share code, notes, and snippets.

@billdawson
Last active December 15, 2015 13:28
Show Gist options
  • Save billdawson/5267259 to your computer and use it in GitHub Desktop.
Save billdawson/5267259 to your computer and use it in GitHub Desktop.
Mixed Javascript/Java Titanium Android module code example. This specific example is to help overcome [TIMOB-13097](https://jira.appcelerator.org/browse/TIMOB-13097).
exports = module.exports = (function() {
var _createExample,
initialized = false;
function checkInit() {
if (!initialized) {
throw "Module not initialized. You must call .init()."
}
}
function test() {
return "Hello from inside Javascript module";
}
function init() {
// Save off the original, Titanium-provided createFactory
// so you can use it (and thus avoid TIMOB-13097),
// then disallow others from using it so you can have full
// control over how Example objects are created (or perhaps
// you always want to return a singleton, etc. etc.)
// In this example, I'm clobbering the original
// createExample(), i.e., I'm absolutely disallowing
// anyone from using mymod.createExample(). But of course
// this is optional.
_createExample = this.createExample;
// Clobber
this.createExample = function() {
throw "createExample() not available. Use exampleFactory() instead.";
};
initialized = true;
return this;
}
function exampleFactory() {
checkInit();
// Calling the original createExample(), so as to
// avoid TIMOB-13097.
// In a real world module, my factory
// method would probably be doing lots of other stuff.
return _createExample();
}
return {
init: init,
test: test,
exampleFactory: exampleFactory
};
})();
//...
@Kroll.method
public ExampleProxy exampleFactory() {
ExampleProxy proxy = new ExampleProxy();
// ... do stuff (or not) to get
// ... proxy ready however you want
// ...
return proxy;
}
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment