1

i have a wizard from a metronic theme where I'm trying to call a function to check if my array contains dublicates.

if I remove this part of my code it works without problems.

console.log(this.checkIfArrayIsUnique()); 

code

var wizard = (<any>$('#m_wizard')).mWizard();        
                    //== Validation before going to next page
                    wizard.on('change', function(wizard) {
                        if(wizard.getStep() > 2){    
                            console.log(this.checkIfArrayIsUnique());                     
                        }
                        mApp.scrollTop();                                        
                    })

Right now my checkIfArrayIsUnique() is just a dummy function

checkIfArrayIsUnique() 
    {
        return true;
    }

how can i call a method outside my 'change' event ? So I'm able to run thru my array and confirm it does not have any dublicates.

JeanPaul A.
  • 3,613
  • 1
  • 20
  • 29
Sonny Hansen
  • 620
  • 1
  • 5
  • 18

2 Answers2

2

the problem is the "function(wizard)" call, since it creates a new scope. But your checkIfArrayIsUnique() ist actually outside of this scope.

Try using ES6 function syntax

wizard.on('change',(wizard) => {
    if(wizard.getStep() > 2){                 
        console.log(this.checkIfArrayIsUnique());                                  
    }
    mApp.scrollTop();                                        
})

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Nicolas Gehlert
  • 2,626
  • 18
  • 39
1

in your change function the variable this point to current function,to use it,you should make this point to out object,you should write like this:

var that = this;
var wizard = (<any>$('#m_wizard')).mWizard();        
//== Validation before going to next page
wizard.on('change', function(wizard) {
    if(wizard.getStep() > 2){    
        console.log(that.checkIfArrayIsUnique());                     
    }
    mApp.scrollTop();                                        
})
LightNow
  • 11
  • 2