Raekye
Raekye

Reputation: 5131

jQuery Mobile - why bind defaults to mobileinit?

I'm using jQuery Mobile and want to turn of Ajax handling because it does some funky stuff with my page. I started with

<script src="jquery..."></script>
<script src="jquerymobile..."></script>
Some other stuff...
My own code:
<script src="config.js"></script>
My other javascript files...

My idea was that you define the global stuff like libraries at the top, and specifics at the bottom so they are rendered later and "overwrite" the global properties. For example, my own CSS would override jQuery's CSS, and my config file would do:

$.mobile.ajaxEnabled = false;

Which successfully disabled the ajax handling.

However, http://jquerymobile.com/demos/1.1.0/docs/api/globalconfig.html says to overwrite defaults in a config file before jQuery mobile is src'd, with some funky notation: (from the page)

<script src="jquery.js"></script>
<script src="custom-scripting.js"></script>
<script src="jquery-mobile.js"></script>

//custom-scripting.js
$(document).bind("mobileinit", function(){
  $.extend(  $.mobile , {
    foo: bar
  });
});

My question is why do that when you can simply include a one-liner $.mobile.ajaxEnabled = false; in a config file after jquery-mobile.js? I assume it's something to do with Javascript's paradigm/style and what not, but I really don't see why you have to go through all that trouble.

So, which one is better to use and why? Why does the documentation recommend this way?

Edit: And the documentation also says mobileinit is called immediately, so I should be safe modifying the $.mobile object later in a config file right?

Upvotes: 0

Views: 445

Answers (1)

Savageman
Savageman

Reputation: 9487

I think I found why. jQuery mobile initialise himself and defines (= overwrites) the $.mobile variable without worrying whether it was already set first. That's explains the mobileinit hook to allow its modification.

Upvotes: 1

Related Questions