I am creating a simple UI commonJS based module which contain various custom UI Modules.
function createCustomView(i) { ... function helper () { //helper function }; } exports.createCustomView = createCustomView;in my window code i will have
custView = ...createCustomView("test");
I have a helper function inside the createCustomView function which allows me to perform certain logic and make changes to internal variables.
I want to be able to call this helper function externally - what would be the way to do this?
i tried custView.helperFunc(); but that does't work.
3 Answers
function createCustomView(i){ this.i = i; // save the arg this.view = Ti.UI.createView({ width : "95%" }); this.text1 = Titanium.UI.createTextField({ keyboardType: Ti.UI.KEYBOARD_NUMBER_PAD }); this.view.add(this.text1); return this; } createCustomView.prototype.helper () { var that = this; that.tex1.blur(); }; exports.createCustomView = createCustomView;
Hi
Tweak your code layout slightly;
function helper() { //helper function } function createCustomView(i) { ... helper(); // you call call the local function here ... } exports.createCustomView = createCustomView; exports.helper = helper; // and expose it externally as well
Try this
Lets consider this as createCustomView.js
function createCustomView(i) { //This variable is accessible only with createCustomView and not externally using the instance var _privateVariable = 'private value'; //This variable is accessible within createCustomView and also outside using the object of createCustomView. See below for example... this.localvariable = ''; //This is a private function available only to createCustomView and not accessible with the object of createCustomView var _privateFunction = function(){ //My Private Implementation goes here. } this.helper = function() { var myLocalvariable = this.localvariable; Ti.API.info(myLocalvariable); Ti.API.info(_privateVariable); }; } module.exports = createCustomView;You can access it from an other class as below: Lets consider another file MyView.js
function MyView(){ var createCustomView = require(path to file + 'createCustomView'); var customView = new createCustomView(); customView.localvariable = 'some value'; customView.helper(); //This will output to the console. //'some value' //'private value' } module.exports = MyView;Hope this helps
Thanks and Regards
Abishek R Srikaanth
Your Answer
Think you can help? Login to answer this question!