Skip to content

Instantly share code, notes, and snippets.

@tcaddy
Created February 21, 2012 21:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcaddy/1879026 to your computer and use it in GitHub Desktop.
Save tcaddy/1879026 to your computer and use it in GitHub Desktop.
Use jQuery Mobile $.mobile.showPageLoadingMsg() for other messages/notifications
var default_msg = $.mobile.loadingMessage; // set this outside function scope so we can access it later
// you must provide a msg argument. The delay and theme arguments are optional.
function show_hide_message = function(msg,delay,theme) {
if (typeof(delay)==='undefined') {
var delay = 3500; // in milliseconds
}
if (typeof(theme)==='undefined') {
var theme = 'a'; // default theme; setting this to 'e' is nice for error messages
}
var css_class = '';
if (theme==='e') {
css_class = 'ui-body-e';
}
$.mobile.loadingMessage = msg;
$.mobile.showPageLoadingMsg();
$(".ui-loader").addClass(css_class).find(".spin:visible").hide(); // hide the spinner graphic
setTimeout(
function() {
$(".ui-loader").removeClass(css_class).find(".spin:hidden").show(); // show the spinner graphic when we are done
$.mobile.hidePageLoadingMsg();
$.mobile.loadingMessage = default_msg; // reset back to default message
},
delay
);
}
@tcaddy
Copy link
Author

tcaddy commented Feb 21, 2012

This was really written as a way to handle Ruby on Rails flash[:notice] messages in a jQuery Mobile way.

The ajax loader message is suitable for other messages/notices. This function will remove the spinner graphic and show a message for a default of 3.5 seconds.

You can optionally override the duration and theme of the message.

@tcaddy
Copy link
Author

tcaddy commented Feb 21, 2012

Note, for the flash[:notice] messages, I used theme 'e', which looks good for error messages.

Also, see my fork of jQuery Mobile which adds support for HTML markup in the loading message.

@ShirtlessKirk
Copy link

If the only reason you need underscore.js is to provide delay() support then window.setTimeout(fn, delay); will do the same as you don't have any extra parameters to pass

@tcaddy
Copy link
Author

tcaddy commented Feb 22, 2012 via email

@heschreib
Copy link

Is there anywhere a link to an adequate RoR example which uses this function?

@tcaddy
Copy link
Author

tcaddy commented Apr 5, 2012

Here is a link to see it in action: http://singapore.highpoint.edu/tix/login

You can simulate a bad login by entering '123' in the barcode field.

You will briefly see the familiar ajax busy dialog for jQuerymobile.

Then you will get an error message about a login failure -- the sort of message that is typical for flash[:notice].

The error message is the ajax busy dialog w/ an alternate message, alternate theme, and with the animated GIF hidden.

@heschreib
Copy link

Hi tcaddy,
I'm a little bit a rails newbie ...
Can you please provide a rails code snippet to call or include thesample above js function in my controller or view. thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment