Skip to content

Instantly share code, notes, and snippets.

@mklymyshyn
Created June 7, 2013 11:18
Show Gist options
  • Save mklymyshyn/5728607 to your computer and use it in GitHub Desktop.
Save mklymyshyn/5728607 to your computer and use it in GitHub Desktop.
// service to display *bootstrap modal* popups
// - `display` method return **promise** which
// contain `resolve` or `cancel` handlers (eq buttons)
common.service('confirmService', ["$q", function($q) {
var that = this;
var deferred = null;
// default state for the modal
this.state = {
message: null,
title: null,
buttons: {
cancel: {title: "Cancel", styles: ""},
resolve: {title: "Save", styles: "btn-primary"}
}};
// hide modal
var destructor = function () {
that.modal.modal("hide");
};
// method to display modal window
// - `title` and `message` are obvious,
// - `conf` may have two properties: `buttons`, and `modal`
// with list of buttons and *bootstrap-modal* settings respectively
this.display = function(title, message, conf) {
var conf = conf || {};
this.modal = $("#modal-window");
that.state.message = message;
that.state.title = title;
that.state.buttons = $.extend(
that.state.buttons, conf.buttons || {});
that.deffered = $q.defer();
var promise = that.deffered.promise;
promise.then(destructor, destructor);
that.modal.modal($.extend(conf.modal || {}, {
backdrop: true,
keyboard: true,
show: false
}));
that.modal.modal("show");
return promise;
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment