Skip to content

Instantly share code, notes, and snippets.

@rblalock
Created October 21, 2011 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rblalock/1304663 to your computer and use it in GitHub Desktop.
Save rblalock/1304663 to your computer and use it in GitHub Desktop.
Simple inheritance with CommonJS Modules and Titanium
/**
* Dashboard controller
*/
// Private
var api = require('parentcontroller');
api.load = function() {
Ti.API.info('Child load method fired');
};
api.getWindow = function() {
Ti.API.info('Child getWindow method fired');
};
// Public
exports.boot = function(_params) {
// expose the public api object
return api;
};
// Create the module
var controllerModule = require('dashboardController').boot(_params);
// Test to see if the parent controller's load method or dashboardController method fires
controllerModule.load();
/**
* Core Controller / Parent Object
*/
// Private
Ti.API.info('Parent controller constructor fired');
// Public
exports.load = function() {
Ti.API.info('Parent load method just fired');
};
exports.build = function() {
Ti.API.info('Parent build method just fired');
};
exports.getWindow = function() {
Ti.API.info('Parent getWindow method just fired');
};
@rblalock
Copy link
Author

This is a really easy pattern to use and requires no extra JS logic or trickery to inherit things, so it's very fast. The downside to this is all overridden methods and properties in the sub controller are gone forever - so you can't use a parent controller method inside the child controller after you've overridden it.

To see how to get around this reference the second example: https://gist.github.com/1304879

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