Getting application controller from application view


#1

Hi, I have the following code. But seems like the this.get(‘controller’) is returning undefined. What is the correct way of getting a application controller from the application view.

didInsertElement: function(){

    //Add swipe listeners
    var hammertime = new Hammer(this.$()[0]);
    hammertime.get('swipe').set({ direction: Hammer.DIRECTION_HORIZONTAL });
    hammertime.on('swipe', function(ev) {
      console.log(ev);

      if(ev.direction === 4) {
        this.get('controller').send('nextPage');
      }
      else {
        this.get('controller').send('previousPage');
      }
    });

#2

I cannot reproduce your issue http://jsfiddle.net/NQKvy/1273/

Edit: one thing that stands out is what is the context of this inside of the swipe callback? You may want to consider using Ember.run.bind there.

Example:

hammertime.on('swipe', Ember.run.bind(this, function(ev) {
      console.log(ev);

      if(ev.direction === 4) {
        this.get('controller').send('nextPage');
      }
      else {
        this.get('controller').send('previousPage');
      }
}));

#3

Or this might help: http://www.quirksmode.org/js/this.html


#4

that’s it, I forgot to bind. Thanks.