How to call a function inside an addEventListener?

0

I would like to know if it is possible to call a function within a given event using addEventListener ().

I have this function, which adds a reward to the database:

    //Função que adiciona uma recompensa ao banco de dados
addReward(){
    if(!this.isEditing){
    this.addDocument(this.caminho, this.recompensa).then(()=>{
      //this.loadData();//refresh view
    });
  }else{
    this.updateDocument(this.caminho, this.recompensa.key, 
    this.recompensa).then(()=>{
      this.loadData();//refresh view
    });
  }
  this.isEditing = false;
  //clear form
  this.recompensa.valor;
  this.recompensa.data;
}

Here is the function that displays a certain ad to the user, I need to have the above function done when I close the video, sending the value of its reward to the database, but it always returns an error: p>

videosInmobi() {
    let carregar = this.loading.create({content : "Carregando..."});
    carregar.present();
    let rewardConfig: AdMobFreeRewardVideoConfig = {
        isTesting: true,
        autoShow: true,
        id: 'ca-app-pub-8000726989219599/6974786599' //id videos InMobi
    };
    this.admob.rewardVideo.config(rewardConfig);
    this.admob.rewardVideo.prepare().then((result) => {
        carregar.dismissAll();
        this.recompensa.data = moment().format('L');
        this.recompensa.valor = 0.03;
        //tento fazer assim atualmente, porém ele sempre retorna um erro quando fecho o vídeo
        document.addEventListener('admob.rewardvideo.events.CLOSE', function(event) {
            console.log(event)
            this.addReward(this.recompensa);
        })
    }), carregar.dismissAll();   
}

The error that returns me is the image below:

    
asked by anonymous 07.11.2018 / 19:41

1 answer

0

Responding directly to the question:

  

How to call a function inside an addEventListener?

The EventTarget.addEventListener () needs at least two parameters. An event type and a listener, which can be an EventListener type object or, more simply , a function as in the example below.

const element = document.querySelector('.js-button')

element.addEventListener('click', function() {
  alert('Obrigado!')
})
<div class="js-button"> CLICA EM MIM </div>

Responding to context:

The error in your code is in calling this.addReward that does not exist. If you give a console.log on this within the addEventListener will see that this method does not exist because this corresponds to the context of the listener.

document.addEventListener('admob.rewardvideo.events.CLOSE', function(event) { console.log(event) this.addReward(this.recompensa); })

I do not know how your code is formatted but maybe just calling addReward(this.recompensa) resolves.

    
07.11.2018 / 21:19