Skip to content

Instantly share code, notes, and snippets.

@KamiKillertO
Created November 14, 2018 15:13
Show Gist options
  • Save KamiKillertO/9a144eeefdae509d668568e2e2710f74 to your computer and use it in GitHub Desktop.
Save KamiKillertO/9a144eeefdae509d668568e2e2710f74 to your computer and use it in GitHub Desktop.
Redirection service v2
import Service from '@ember/service';
import { set } from '@ember/object';
import { inject as service } from '@ember/service';
export default Service.extend({
router: service(),
__route: null,
displayModal: false,
__setUpRedirection(url) {
set(this, '__route', url);
set(this, 'displayModal', true);
this.router.addObserver('currentRouteName', () => {
this.__cancelRedirection();
});
},
actions: {
redirect(event) {
event.preventDefault();
let target = event.target;
while (target.tagName !== 'A') {
target = target.parentNode;
}
this.__setUpRedirection(target.href);
},
proceedRedirection() {
window.location = this.__route;
},
cancelRedirection() {
this.__cancelRedirection();
}
},
__cancelRedirection() {
set(this, '__route', null);
set(this, 'displayModal', false);
this.router.removeObserver('currentRouteName');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment