Skip to Content
0

how to call a function from another function

Dec 22, 2016 at 09:32 PM

106

avatar image
Former Member

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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Jun Wu Dec 23, 2016 at 02:55 AM
0

why not

 formatter.test()



var displayColor = this.evaluateColorFormat(DLAPrice, proposedPrice); 
Show 1 Share
10 |10000 characters needed characters left 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.

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

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);
Show 1 Share
10 |10000 characters needed characters left 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.

0