2

For example, I am using jQuery validation plugin which provides a way to set the default settings using jQuery.validator.setDefaults(). I would like these settings and other things to be used for my entire application.

$(document).on('mobileinit', function(){
    jQuery.ajaxSetup({ ... });

    $.fn.pluginName = function() { ... };

    jQuery.validator.setDefaults({ ... });
    jQuery.validator.addMethod(...);
});

$(document).on('pagecreate', '#register', function(){ ... });

$(document).on('pagecreate', '#login', function(){ ... });
  • Use `pageinit` as the mobile equivalent of DOM ready. See: http://stackoverflow.com/a/17529809/594235 – Sparky Apr 17 '15 at 04:42
  • 1
    `pageinit` is deprecated in jQuery 1.4.0 in favor of `pagecreate`. I do not want to set the defaults in `pagecreate` because it will be triggered each time a page is created. I would like to set the defaults once for the entire application. – Lynn Adrianna Apr 17 '15 at 05:26
  • I think you meant to say jQuery "Mobile" 1.4, however, I don't see where you mentioned any versions in your OP. Nor have you stated which method is being used to load pages/content. JavaScript needs to be loaded for each document. You cannot load a new document without reloading the JavaScript, unless you're loading one document and then loading content for each "page" into that single document with `ajax`. See: http://stackoverflow.com/a/14469041/594235 – Sparky Apr 17 '15 at 14:49

1 Answers1

1

No, it wouldn't be the best place. The mobileinit event is fired pretty much as soon as the jquerymobile javascript file is loaded. If you've got other plugins included after jquerymobile, they won't be initialized when you go to use them.

That aside, if it's sensible to do so, you should keep your individual plugin's logic separate from one another, anyway. It's easier to read and understand, and is arguably more robust.

Example:

$(document).on('mobileinit', function(){
    jQuery.ajaxSetup({ ... });

    $.fn.pluginName = function() { ... };

    jQuery.validator.setDefaults({ ... });
    jQuery.validator.addMethod(...);
});
<script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.min.js"></script>

It's likely here that the jquerymobile script will be loaded before the jquery.validate script, causing an error when you go to use it.

Fiddles
  • 2,790
  • 1
  • 32
  • 35
  • Alright, but I'm still unsure where to place global configurations. Ignoring the plugins, if I want to set the defaults for `jQuery.validator` for my entire application, where would be the best place to have it? – Lynn Adrianna Apr 17 '15 at 03:48
  • Depends where it belongs, I guess. Often cases the best place to put it is in the document ready event. JqueryMobile is a different beast, with the way it loads in pages (might be worth looking over http://stackoverflow.com/a/14469041/405180), but the premise is the same. – Fiddles Apr 17 '15 at 04:00
  • Just for a bit more info, Jquerymobile has the `mobileinit` event specially, as it uses these settings within it's actual setup, after the $.mobile object has been created, but before it goes about modifying the page. – Fiddles Apr 17 '15 at 04:06