Module Helper functions

You must Login before you can answer or comment on any questions.

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

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
4 Comments
  • I will need to access variables inside function createCustomView - will I be able to that in this way?

    — commented 10 months ago by Name Surname

  • Please provide an example of your code and explain what exactly what you are trying to achieve.

    — commented 10 months ago by Malcolm Hollingsworth

  • function createCustomView(i){
     
    var self = Ti.UI.createView({
            width : "95%"
        });
     
     
    var text1 = Titanium.UI.createTextField({
                keyboardType: Ti.UI.KEYBOARD_NUMBER_PAD
            });
     
    self.add(text1);
     
    return self;
     
    }
    I want to call text1.blur so that i can hide the keypad

    — commented 10 months ago by Name Surname

  • Show 1 more comment

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!