Created
October 30, 2014 18:26
-
-
Save avaz/10d909038fc10b3290f2 to your computer and use it in GitHub Desktop.
An approach to Bootstrap Modals in EmberJS using Mixins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import ModalSupport from '../mixins/modal-support'; | |
export default Ember.Controller.extend(ModalSupport, { | |
actions: { | |
tryModal: function() { | |
var _this = this; | |
_this.modalFor({template: 'some-template', | |
title: 'Some Title'}) | |
.then(function(){ | |
console.log('action ok was clicked'); | |
}) | |
.catch(function() { | |
console.log('action cancel was clicked'); | |
}); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Mixin.create({ | |
modal: null, | |
okDecision: null, | |
cancelDecision: null, | |
okDisabled: false, | |
cancelDisabled: false, | |
title: '', | |
okLabel: '', | |
cancelLabel: '', | |
toggleOk: function() { | |
this.toggleProperty('okDisabled'); | |
}, | |
toggleCancel: function() { | |
this.toggleProperty('cancelDisabled'); | |
}, | |
modalFor: function(options) { | |
this.set('title', options.title || ''); | |
this.set('okLabel', options.okLabel || 'Confirmar'); | |
this.set('okDisabled', options.okDisabled || false); | |
this.set('cancelLabel', options.cancelLabel || 'Cancelar'); | |
this.set('cancelDisabled', options.cancelDisabled || false); | |
var modal = this.container.lookup('view:modal'); | |
modal.set('controller', this); | |
modal.set('templateName', options.template); | |
modal.append(); | |
var self = this; | |
self.set('modal', modal); | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
self.set('okDecision', resolve); | |
self.set('cancelDecision', reject); | |
}); | |
}, | |
disposeModal: function() { | |
this.get('modal').hide(); | |
this.set('okDecision', null); | |
this.set('cancelDecision', null); | |
this.set('modal', null); | |
}, | |
actions: { | |
okAction: function() { | |
this.get('okDecision')(); | |
this.disposeModal(); | |
}, | |
cancelAction: function() { | |
this.get('cancelDecision')(); | |
this.disposeModal(); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.View.extend({ | |
layoutName: 'modal', | |
hide: function() { | |
this.$('.modal').modal('hide'); | |
}, | |
show: function() { | |
this.$('.modal').modal({backdrop:'static'}).on('hidden.bs.modal', function() { | |
this.destroy(); | |
}.bind(this)); | |
}.on('didInsertElement') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I really like this approach, especially with a Promise return, that allows to use this modal with transitions as polyfill for
beforeunload
event.