Created
October 21, 2011 19:10
-
-
Save rblalock/1304663 to your computer and use it in GitHub Desktop.
Simple inheritance with CommonJS Modules and Titanium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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