Skip to content

Instantly share code, notes, and snippets.

@ppcano
Created May 20, 2014 17:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppcano/7a5f0e551cd9b2db42b0 to your computer and use it in GitHub Desktop.
Save ppcano/7a5f0e551cd9b2db42b0 to your computer and use it in GitHub Desktop.
hammerJS + ember integration
Em.View.reopen({
hammerOptions: null,
enableHammer: function() {
var gestures = ['drag', 'hold', 'release', 'swipe', 'tap', 'touch', 'transform'];
var options = this.get('hammerOptions');
if ( options ) {
gestures.forEach(function(gesture) {
if ( !options[gesture] ) {
options[gesture] = false;
}
});
this.hammer = new Hammer(this.get('element'), options);
}
}.on('didInsertElement'),
destroyHammer: function() {
if (this.hammer) {
this.hammer.dispose();
this.hammer = null;
}
}.on('willDestroyElement')
});
Ember.EventDispatcher.reopen({
events: {
tap : 'tap',
click : 'click', // manage to preventDefault link-to views
focusin : 'focusIn',
focusout : 'focusOut',
submit : 'submit',
input : 'input',
change : 'change'
},
_findNearestEventManager: function() {
return null;
}
});
Ember.LinkView.reopen({
hammerOptions: {
tap: true
},
eventName: 'tap',
click: function(event) {
event.preventDefault();
}
});
@mohamnag
Copy link

mohamnag commented Aug 5, 2014

sorry for dummy question but I dont find how can I handle the events like swipe in my view. I tried to define a handler for swipe like this:

App.MyView = Ember.View.extend({
    hammerOptions: {
        swipe: true,
        tap: true
    },
    tap: function() {
        console.log('tap');
    },
    swipe: function() {
        console.log('swipe');
    }
});

but non of the tap nor swipe events ever fire.

Im using ember 1.6.1 and hammer 2.0.2

@ppcano
Copy link
Author

ppcano commented Aug 5, 2014

@mohamnag, below a possible integration. You could also ask in stackoverflow:

setupTap: function() {
this._super();

var self = this;
this.hammer = new Hammer(this.get('element'), {recognizers: []});

var tap = new Hammer.Tap();

tap.set('enable', function() {
  return self.canRecognizeTap();
});
this.hammer.add(tap);

this.hammer.on('tap', function() {
  self.tap();
});

}.on('didInsertElement')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment