#178 Close dynamic modals programmatically

This commit is contained in:
Noel De Martin
2018-02-21 16:42:01 +01:00
parent 967745c3cd
commit 75ac5598fe
4 changed files with 38 additions and 10 deletions

View File

@@ -177,6 +177,19 @@ this.$modal.show(MyComponent, {
}) })
``` ```
Other than defining the `name` modal parameter, it's also possible to close dynamic modals emitting a `'close'` event:
```javascript
this.$modal.show({
template: `
<div>
<p>Close using this button:</p>
<button @click="$emit('close')">Close</button>
</div>
`
})
```
For more examples please take a look at [vue-js-modal.yev.io](http://vue-js-modal.yev.io). For more examples please take a look at [vue-js-modal.yev.io](http://vue-js-modal.yev.io).
### SSR ### SSR

View File

@@ -225,7 +225,7 @@ export default {
showDynamicRuntimeModal () { showDynamicRuntimeModal () {
this.$modal.show({ this.$modal.show({
template: ` template: `
<div> <div class="example-modal-content">
<h1>This is created inline</h1> <h1>This is created inline</h1>
<p>{{ text }}</p> <p>{{ text }}</p>
</div> </div>
@@ -243,9 +243,19 @@ export default {
}, },
showDynamicComponentModalWithModalParams () { showDynamicComponentModalWithModalParams () {
this.$modal.show(CustomComponentModal, { this.$modal.show({
text: 'This text is passed as a property' template: `
}, { <div class="example-modal-content">
<button class="btn" @click="closeByName">Close this using name</button>
<button class="btn" @click="closeByEvent">Close this using events</button>
</div>
`,
methods: {
closeByName() { this.$modal.hide('dynamic-modal'); },
closeByEvent() { this.$emit('close'); },
}
}, null, {
name: 'dynamic-modal',
resizable: true, resizable: true,
adaptive: true, adaptive: true,
draggable: true, draggable: true,

View File

@@ -1,5 +1,5 @@
<template> <template>
<div> <div class="example-modal-content">
<h1>This is a custom component</h1> <h1>This is a custom component</h1>
<p>{{ text }}</p> <p>{{ text }}</p>
</div> </div>

View File

@@ -3,11 +3,14 @@
<modal <modal
v-for="modal in modals" v-for="modal in modals"
:key="modal.id" :key="modal.id"
:name="modal.name"
v-bind="modal.config" v-bind="modal.config"
@closed="remove(modal.id)" @closed="remove(modal.id)"
> >
<component :is="modal.component" v-bind="modal.params"></component> <component
:is="modal.component"
v-bind="modal.params"
@close="$modal.hide(modal.config.name)"
></component>
</modal> </modal>
</div> </div>
</template> </template>
@@ -25,16 +28,18 @@ export default {
methods: { methods: {
add (modal, params, config) { add (modal, params, config) {
let id = this.uid++ let id = this.uid++
let name = '_dynamic-modal-' + id config = config || {};
if (!config.name) {
config.name = '_dynamic-modal-' + id;
}
this.modals.push({ this.modals.push({
id: id, id: id,
name: name,
component: modal, component: modal,
params: params || {}, params: params || {},
config: config || {} config: config || {}
}) })
this.$nextTick(() => { this.$nextTick(() => {
this.$modal.show(name) this.$modal.show(config.name)
}) })
}, },
remove (id) { remove (id) {