dimanche 12 août 2018

Basic use of `this` in Ember.js

I'm fairly adept in Javascript but I'm having a hard time understanding how Ember is handling the this context in certain scenarios.

I have a component controller:

import Component from '@ember/component';

export default Component.extend({
  keyPress(e) {
    // here I want to call a method in the `actions` hash
    this.get('onAccept')
  },

  actions: {
    onAccept() {
      console.log('action accepted!')
    }
  }
}

Every time I run this though, I get the following error:

Uncaught TypeError: this.get(...) is not a function

This seems to always happen when I have a method outside of the actions hash that needs to access a function inside the actions hash, or the other way around.

And this behavior seems necessary, because component events belong outside of the actions hash.

So how am I supposed to have event methods outside of the actions hash but still be able to call methods that belong inside of the actions hash?

This seems to be a poorly documented part of controllers in Ember. Maybe I am just missing something here. Any help is greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire