OK to go ahead and share {{app}}?
30 | 31 | ``` 32 | 33 | ```js 34 | Meteor.startup(function(){ 35 | var shareDialogInfo = { 36 | template: Template.appShareDialog, 37 | title: "Share the app", 38 | modalDialogClass: "share-modal-dialog", //optional 39 | modalBodyClass: "share-modal-body", //optional 40 | modalFooterClass: "share-modal-footer",//optional 41 | removeOnHide: true, //optional. If this is true, modal will be removed from DOM upon hiding 42 | buttons: { 43 | "cancel": { 44 | class: 'btn-danger', 45 | label: 'Cancel' 46 | }, 47 | "ok": { 48 | closeModalOnClick: false, // if this is false, dialog doesnt close automatically on click 49 | class: 'btn-info', 50 | label: 'Ok' 51 | } 52 | 53 | }, 54 | doc: { // Provide data context for Template.appShareDialog 55 | app: "My Application" 56 | } 57 | } 58 | 59 | var rd = ReactiveModal.initDialog(shareDialogInfo); 60 | 61 | }); 62 | ``` 63 | 64 | ###button event handling 65 | ```js 66 | rd.buttons.ok.on('click', function(button){ 67 | // what needs to be done after click ok. 68 | }); 69 | ``` 70 | 71 | ###Displaying Modal 72 | you can use show/hide methods to show/hide modal 73 | ```js 74 | rd.show(); 75 | ``` 76 | 77 | ```js 78 | rd.hide(); 79 | ``` 80 | ### capture modal html element 81 | ```javascript 82 | //modalTarget contains the html 83 | $(rd.modalTarget).find('[name=inputFooBar]').val() 84 | ``` 85 | 86 | ### Modal template data context 87 | 88 | Provide a `doc` property on the info options object to provide a data context for your dialog template 89 | 90 | ### License 91 | MIT 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /lib/reactive-modal.js: -------------------------------------------------------------------------------- 1 | var _modals = {}; 2 | ReactiveModal = function(){ 3 | // EV.call(this); 4 | this.buttons = {}; 5 | }; 6 | // _.extend(ReactiveModal.prototype, EV.prototype); 7 | Template._reactiveModal.helpers({ 8 | class: function(){ 9 | var att; 10 | if(this.class){ 11 | att = 'btn ' + this.class; 12 | } 13 | return att; 14 | }, 15 | disabled: function(){ 16 | if (this.button.disabled.get()) { 17 | return "disabled" 18 | } else { 19 | return null; 20 | } 21 | }, 22 | dismiss: function(){ 23 | if(this.button.closeModalOnClick){ 24 | return "modal"; 25 | } 26 | }, 27 | arrayify: function(obj){ 28 | result = []; 29 | for (var key in obj) result.push({name:key,button:obj[key]}); 30 | return result; 31 | } 32 | }); 33 | 34 | Template._reactiveModal.events({ 35 | 'click .modal-footer .reactive-modal-btn.btn': function(e){ 36 | this.button.emit('click', this.button); 37 | } 38 | }); 39 | 40 | ReactiveModal.initDialog = function (info){ 41 | var key = "rm-"+Meteor.uuid(); 42 | if(!info || !info.template){ 43 | console.error("you must define a template for " , key); 44 | } else { 45 | info.key = key; 46 | _modals[key] = info; 47 | 48 | for(var button in info.buttons){ 49 | var newButton = _.clone(info.buttons[button]); 50 | _.extend(newButton, new EV()); 51 | info.buttons[button] = newButton; 52 | newButton.closeModalOnClick = (info.buttons[button].closeModalOnClick === undefined || info.buttons[button].closeModalOnClick === true) ? true : false; 53 | newButton.disabled = new ReactiveVar((info.buttons[button].disabled === undefined || info.buttons[button].disabled === false || (info.buttons[button].disabled instanceof ReactiveVar && info.buttons[button].disabled.curValue===false)) ? false : true); 54 | newButton.disable = function () {newButton.disabled.set(true);}, 55 | newButton.enable = function () {newButton.disabled.set(false);} 56 | }; 57 | 58 | Blaze.renderWithData(Template._reactiveModal, info, document.body); 59 | } 60 | 61 | var modalTarget = $('#' + key); 62 | info.show = function(){ 63 | modalTarget.modal('show'); 64 | } 65 | info.hide = function(){ 66 | modalTarget.modal('hide'); 67 | } 68 | 69 | if (info.removeOnHide) { 70 | modalTarget.on('hidden.bs.modal', function() { 71 | $(this).remove(); 72 | }); 73 | } 74 | 75 | info.modalTarget = modalTarget; 76 | return info; 77 | }; 78 | --------------------------------------------------------------------------------