Skip to Content
avatar image
Former Member

how to call a function from another function

I have my controller file in which I call a function on another file, then from that function I wan to call another function within that file but I'm unable to do that.

controller.js code

onPricePress: function(evt) {
   //some code here
   ....
  //now what I want here
   formatter.test.apply(this); //formatter is the reference to my other file and calls the function test().
  
}

formatter.js code

test: function(){
//some code here
....
//now what I want

var displayColor = evaluateColorFormat(DLAPrice, proposedPrice); //this is my other function

},
evaluateColorFormat(DLAPrice, proposedPrice){

//do something
}

so I want to call evaluateColorFormat() function from test() but I cannot do that, it says the reference to that function doesn't exist. I tried using this.function() format but it gives me the same error. I saw a solution on stackoverflow but that makes use of this.function() which doesn't work for me. Any ideas anyone?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Dec 23, 2016 at 02:55 AM

    why not

     formatter.test()
    
    
    
    var displayColor = this.evaluateColorFormat(DLAPrice, proposedPrice); 
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Already tried that and doesn't work. The reason for it not working is that when I call formatter.test() from my controller I attach "this" to it, which means that test() has a controller object reference, so when using this.something() inside formatter.test() it points to the controller object, which doesn't have the evaluateColorFormat() function.

  • avatar image
    Former Member
    Dec 23, 2016 at 04:33 PM

    Hi,

    Can you paste your entire Formatter. I solved your problem like this :

    MyFormatter:

    jQuery.sap.declare("model.formatter");
    
    model.formatter = {
               
            testFunc:function(a,b) {
                console.log('called');
                model.formatter.testFunc2();
    
            },
            testFunc2: function () {
                console.log('called func 2');
            }
    };
    
    
    

    And call in my Controller is same as yours :

    this.formatter.testFunc.apply(this);
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      I found a solution to this. The solution above would work but I was calling the first function from another file. I found a way to make it work already thanks.