Talking to your users is kind of a big deal in an interactive UI. Jaffa has a couple of options here, depending on what you are trying to do.
In the case where you want to display a specific message using jQuery, Jaffa offers a trivial wrapper for handling all of the open/close logic. It can be called like so:
jaffa.ui.messageBox("My message");
Pretty basic, and a simple alternative to a normal
alert() call. You can also provide
a title for the window if you'd like:
jaffa.ui.messageBox("My message", "My title");
If you'd like to get a little more fancy you can request Jaffa executes a callback function after the user presses 'ok' as well. Jaffa will double-check that this is in fact a function, otherwise treating it as a title:
jaffa.ui.messageBox("My message", msgBoxCallback);
As a final example, combining all three, the example below shows a trivial test. The text you type in the left box will appear in the dialog, and anything you write in the right box will be overwritten in the callback function after you press 'OK':
function msgBoxCallback() {
$("#msg-box-test-output").val("This text was written by a callback function. You can provide this to execute after the user closes a message dialog.")
}
$("#msg-box-test-btn").click(function() {
jaffa.ui.messageBox($("#msg-box-test-text").val(), msgBoxCallback, "Demonstration Message Box");
});
The other relevant point here is that you can control how Jaffa provides feedback to the user during the course of normal operations. You may want to do this from a branding perspective, or simple to copy/redirect messages to alternate logging systems. You can provide this during instantiation:
function myCustomAlert(message) {
// Do your stuff here
}
var jaffa = jaffaFactory({
functionUserFeedback: myCustomAlert
});
Or you can modify the value in memory during execution:
jaffa.userConfig.functionUserFeedback = function(message) {
// Do your stuff here
}
During the unit tests for this page we use the second method to deliberately suppress the messages when we force a failure for testing, then reinstate normal operations when the test completes. Like so:
var alertGenerated = false;
function deliberateFail() {throw "This is a forced failure inside a unit test";}
var backupDialog = jaffa.userConfig.functionUserFeedback;
jaffa.userConfig.functionUserFeedback = function(message) {alertGenerated = true;}
jaffa.util.callIfFunction(deliberateFail);
jaffa.userConfig.functionUserFeedback = backupDialog;
if (alertGenerated === false) {
// Logic for test failing
}