Skip to content

Instantly share code, notes, and snippets.

@pierr
Created June 30, 2014 15:16
Show Gist options
  • Save pierr/5c47a2b35d9691cb6303 to your computer and use it in GitHub Desktop.
Save pierr/5c47a2b35d9691cb6303 to your computer and use it in GitHub Desktop.
confirm.js
/* global _ , window, Promise */
(function (NS) {
"use strict";
//Filename: helpers/message_helper.js
var isInBrowser = typeof module === 'undefined' && typeof window !== 'undefined';
//create a modal in the DOM.
var ModalView = isInBrowser ? NS.Views.ModalView : require('../views/modal-view');
var confirmView = new ModalView({ modalTitle: i18n.t('modal.confirm'), templateConsult: function () { return "" } });
//if (!$('div[data-modal-confirm]').length) {
//Register the modal in the DOM.
$('body').append(confirmView.render().el);
//Create an event manager.
var evtManager = _.extend({}, Backbone.Events);
//Confirm promise version.
var confirm = function messageHelperConfirm(messageKey) {
confirmView.modalTitle = i18n.t(messageKey);
confirmView.render();
confirmView.showModal();
return new Promise(function (resolve, failure) {
//Listen to once to the close event of the modal.
evtManager.listenToOnce(confirmView, "modal:close", function (data) {
eventManager.stopListening(confirmView, 'modal:close');
resolve(messageKey);
});
//Listen to once the cancel event on the modal.
evtManager.listenToOnce(confirmView, "modal:cancel", function (data) {
eventManager.stopListening(confirmView, 'modal:close');
reject(messageKey);
});
});
if (isInBrowser) {
// TODO : implement another confirm method (currently we are using native javascript confirm function)
return window.confirm(i18n.t(messageKey));
}
return false;
};
//Message helper.
var messageHelper = {
confirm: confirm
};
if (isInBrowser) {
NS.Helpers = NS.Helpers || {};
NS.Helpers.messageHelper = messageHelper;
} else {
module.exports = messageHelper;
}
})(typeof module === 'undefined' && typeof window !== 'undefined' ? window.Fmk : module.exports);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment