Created
June 30, 2014 15:16
-
-
Save pierr/5c47a2b35d9691cb6303 to your computer and use it in GitHub Desktop.
confirm.js
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
/* 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