Skip to content

Commit

Permalink
Add onceClose/onceOpen methods in Modal module and make events mo…
Browse files Browse the repository at this point in the history
…re reliable
  • Loading branch information
artf committed Mar 13, 2019
1 parent e1d341a commit d809b5b
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/modal_dialog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* * [getTitle](#gettitle)
* * [setContent](#setcontent)
* * [getContent](#getcontent)
* * [onceClose](#onceclose)
* * [onceOpen](#onceopen)
*
* @module Modal
*/
Expand All @@ -32,6 +34,10 @@ module.exports = () => {
ModalView = require('./view/ModalView');
var model, modal;

const triggerEvent = (enable, em) => {
em && em.trigger(`modal:${enable ? 'open' : 'close'}`);
};

return {
/**
* Name of the module
Expand All @@ -55,11 +61,13 @@ module.exports = () => {
...config
};

this.em = c.em;
const em = c.em;
this.em = em;
var ppfx = c.pStylePrefix;
if (ppfx) c.stylePrefix = ppfx + c.stylePrefix;

model = new ModalM(c);
model.on('change:open', (m, enb) => triggerEvent(enb, em));
modal = new ModalView({
model,
config: c
Expand All @@ -73,11 +81,6 @@ module.exports = () => {
this.render().appendTo(el);
},

triggerEvent(event) {
const { em } = this;
em && em.trigger(`modal:${event}`);
},

/**
* Open the modal window
* @param {Object} [opts={}] Options
Expand All @@ -89,7 +92,6 @@ module.exports = () => {
opts.title && this.setTitle(opts.title);
opts.content && this.setContent(opts.content);
modal.show();
this.triggerEvent('open');
return this;
},

Expand All @@ -99,7 +101,28 @@ module.exports = () => {
*/
close() {
modal.hide();
this.triggerEvent('close');
return this;
},

/**
* Execute callback when the modal will be closed.
* The callback will be called one only time
* @param {Function} clb
* @returns {this}
*/
onceClose(clb) {
this.em.once('modal:close', clb);
return this;
},

/**
* Execute callback when the modal will be opened.
* The callback will be called one only time
* @param {Function} clb
* @returns {this}
*/
onceOpen(clb) {
this.em.once('modal:open', clb);
return this;
},

Expand Down

0 comments on commit d809b5b

Please sign in to comment.