Hello @jthoburn,
thanks for your kind offer!
I just got it working by myself. I’m just unsure if it is the best way to do it:
- Create a gestures service to hold the swipeLeft and swipeRight state
- Create a component which wraps everything in your application template and listens to the swipe events
- Inject the service into the app-wrapper component. Toggle the services swipe states each time one of these events occur
- Inject the service also wherever you need to react to the swipe events
- Observe the services state variables and do your task
ember g service gestures
gestures/service.js:
import Ember from 'ember';
export default Ember.Service.extend({
swipeLeftToggle: false,
swipeRightToggle: false,
swipeLeftObserver: Ember.observer('swipeLeftToggle', function() {
this.toggleProperty('_swipeLeft');
}),
swipeRightObserver: Ember.observer('swipeRightToggle', function() {
this.toggleProperty('_swipeRight');
}),
_swipeLeft: true,
pulseSwipeLeft: Ember.computed.oneWay('_swipeLeft').readOnly(),
_swipeRight: true,
pulseSwipeRight: Ember.computed.oneWay('_swipeRight').readOnly(),
});
ember g component app-wrapper
application/template.hbs:
{{#app-wrapper}}
{{outlet}}
{{/app-wrapper}}
components/app-wrapper/component.js:
import RecognizerMixin from 'ember-gestures/mixins/recognizers';
import Ember from 'ember';
export default Ember.Component.extend(RecognizerMixin, {
gestures: Ember.inject.service(),
recognizers: 'swipe',
swipeLeft() {
this.toggleProperty('gestures.swipeLeftToggle');
},
swipeRight() {
this.toggleProperty('gestures.swipeRightToggle');
}
});
page/controller.js:
import Ember from 'ember';
export default Ember.Controller.extend({
gestures: Ember.inject.service(),
swipeLeftBinding: 'gestures.pulseSwipeLeft',
swipeLeftObserver: Ember.observer('swipeLeft', function() {
let nextModel = this.store.peekRecord('page', parseInt(this.get('model.id')) + 1);
if (nextModel !== null) {
this.transitionToRoute('/page/' + nextModel.get('id'));
}
}),
swipeRightBinding: 'gestures.pulseSwipeRight',
swipeRightObserver: Ember.observer('swipeRight', function() {
let prevModel = this.store.peekRecord('page', parseInt(this.get('model.id')) - 1);
if (prevModel !== null) {
this.transitionToRoute('/page/' + prevModel.get('id'));
}
})
});
Edit: I also tried it with the swipeUp and swipeDown events. However, they were never fired. Nevermind, I don’t need them atm.