Niklas
Niklas

Reputation: 465

I'm getting this.get() is undefined in ember

I'm trying to create an event bus to be able to trigger some ui events throughout my app. I've followed the instructions here: http://emberigniter.com/communication-between-distant-components/

I load my event bus as a service into a component property called ui but for some reason I get this error: TypeError: this.get(...) is undefined, which is weird, since it should be, right? It doesn't matter if I run the code in init or later in didInsertElement, same error. What can I do?

// services/ui.js

import Ember from 'ember';

export default Ember.Service.extend(Ember.Evented);

//components/side-menu.js

import Ember from 'ember';

export default Ember.Component.extend(Ember.Evented, {
  ui: Ember.inject.service(),
  init() {
    this._super(...arguments);
    this.get('ui').on('side-menu:toggle', this.actions, 'toggle');
  },
  actions: {
    toggle: function() {
      this.$().sidebar('toggle');
    }
  }
});

Upvotes: 1

Views: 3666

Answers (1)

Niklas
Niklas

Reputation: 465

I figured it out. It was because I was binding this.actions and get wasn't defined on the actions property, I guess. When I instead made a method called toggle and bound this instead it worked. Thanks for the help!

Upvotes: 1

Related Questions