Skip to content

Instantly share code, notes, and snippets.

@sukima
Last active April 2, 2019 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sukima/ef3ee50b0d166d6912eb6f6ea2cbb577 to your computer and use it in GitHub Desktop.
Save sukima/ef3ee50b0d166d6912eb6f6ea2cbb577 to your computer and use it in GitHub Desktop.
Global modal warning
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
export default Controller.extend({
warningModal: service(),
actions: {
warningModalResolve() {
this.warningModal.resolve(...arguments);
}
}
});
import Mixin from '@ember/object/mixin';
import { inject as service } from '@ember/service';
export default Mixin.create({
warningModal: service(),
activate() {
this.warningModal.show()
.onCanceled(() => this.transitionTo('index'));
return true;
}
});
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('foo');
this.route('bar');
this.route('baz');
});
export default Router;
import Route from '@ember/routing/route';
import WarningRouteMixin from '../mixins/warning-route';
export default Route.extend(WarningRouteMixin, {
});
import Route from '@ember/routing/route';
export default Route.extend({
});
import Route from '@ember/routing/route';
import WarningRouteMixin from '../mixins/warning-route';
export default Route.extend(WarningRouteMixin, {
});
import Route from '@ember/routing/route';
export default Route.extend({
});
import Service from '@ember/service';
import { bool } from '@ember/object/computed';
import { tryInvoke } from '@ember/utils';
import Confirmer from 'confirmer';
export default Service.extend({
isOpen: bool('resolver'),
show() {
return new Confirmer(resolver => {
this.set('resolver', resolver);
}).onDone(() => {
this.set('resolver', null);
});
},
resolve(method, value) {
tryInvoke(this.resolver, method, [value]);
}
});
<nav class="nav">
{{link-to "Index" "index" class="nav-link"}}
{{link-to "Foo" "foo" class="nav-link"}}
{{link-to "Bar" "bar" class="nav-link"}}
{{link-to "Baz" "baz" class="nav-link"}}
</nav>
{{outlet}}
{{#if this.warningModal.isOpen}}
{{#bs-modal
onSubmit=(action "warningModalResolve" "confirm")
onHide=(action "warningModalResolve" "cancel")
as |modal|
}}
{{#modal.header}}
<h4 class="modal-title">
<i class="glyphicon glyphicon-alert"></i>
Alert
</h4>
{{/modal.header}}
{{#modal.body}}
Are you absolutely sure you want to do that???
{{/modal.body}}
{{#modal.footer}}
{{#bs-button onClick=(action modal.close) type="danger"}}
Oh no, forget it!
{{/bs-button}}
{{#bs-button onClick=(action modal.submit) type="success"}}
Yeah!
{{/bs-button}}
{{/modal.footer}}
{{/bs-modal}}
{{/if}}
<h1>Bar Route</h1>
<p>This route <strong>does</strong> have the warning.</p>
<h1>Baz Route</h1>
<p>This route does <strong>not</strong> have the warning.</p>
<h1>Foo Route</h1>
<p>This route <strong>does</strong> have the warning.</p>
<h1>Index Route</h1>
<p>This route does <strong>not</strong> have the warning.</p>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-bootstrap": "2.6.0",
"ember-confirmed": "2.5.1",
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment