Adds API-like functionality for rendering and managing modals in your Ember application.
This addon tracks the context from where you showed a modal, allowing you to easily interact with your current route, component, or controller from within the modal.
ember install ember-modalsAnd add the {{ember-modals}} component in your application template:
To show a modal using a HTMLBars action, call showModal and pass the name of any component to render as a modal.
You must specify target as modals. The target will reference the modals service, which is injected into routes, components, and controllers by default.
The showModal action accepts a second, optional parameter, context:
When you pass a context, this will be set as the targetObject of the component you passed.
Instead of passing a component and context to the showModal action, you can pass a single options object.
This object supports more options that name-and-context approach mentioned above:
/* Within some route, component, or controller... */
this.modals.send('showModal', {
componentName: 'my-welcome-dialog',
context: this,
modalClassName: 'welcome-modal',
overlayClassName: 'overlay-transparent',
showCloseButton: true,
});Congratulations! You shiney new modal has been render in the DOM!
Because you rendered it from within some component or route, you might want to access properties or actions on that class. To do this, just access the targetObject property in your component or component's layout:
/* Some route, component, or controller... */
export default Ember.Component.extend({
userName: 'Dave',
actions: {
checkWeCanDeleteThis() {
this.modals.showModal('confirm-delete', this);
},
confirmDelete() {
this.get('model').deleteRecord();
}
},
});Note two things:
- You must have passed
contexttoshowModal()as described in passing modal options** - Set
target=targetObjectto call actions on the route or component you rendered the modal from
You can also access the modal property, giving you access to the original options to passed to showModal():
There are three ways modals can be closed:
- Hitting
esc - Clicking on the overlay
- By sending the
closeModalaction from within the modal content: