Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating templated/persistant header/footer template in jQuery Mobile and PhoneGap

I'm diving into writing a mobile app with jQuery Mobile/PhoneGap. I'm using this sample template to start from, which uses HTML/JS to create the pages. Rather than have all the <page> tags in one single html file, he's got it split up so it's easier to edit.

Since I will have a separate file for each page, what's the best way to include the tempated header/footer? I've only seen it where you need to copy and paste the whole footer->navbar code into each HTML page. This doesn't seem like it should be. For example, if you want to change one menu item, you need to go into each page and change it.

What's the solution that I'm missing?

Maybe I'm just not understanding jQuery Mobile. For example, their sidebar they use for their docs - is that sidebar code copy and pasted onto each page? That doesn't make sense. It's the same idea as the question I'm asking here about the footer.

http://jquerymobile.com/test/docs/pages/page-cache.html

This is what I've got that doesn't seem right (and $.live('pageinit') isn't working). This HTML is what goes on each HTML page:

<div id="mainFooter" data-position="fixed" data-id="mainFooter" data-role="footer" class="ui-footer ui-bar-a ui-footer-fixed fade ui-fixed-inline" role="contentinfo" style="top: 58px;"> 

And the JS

$.live('pageinit', function (event) {     displayFooter(); });  function displayFooter() {     $('#mainFooter').html('<div data-role="navbar" class="nav-glyphish-example" data-grid="d">' +         '<ul>' +         '<li><a href="#" id="menuitem1" data-icon="custom">Menu Item 1</a></li>' +         '<li><a href="#" id="menuitem2" data-icon="custom">Menu Item 2</a></li>' +         '<li><a href="#" id="menuitem3" data-icon="custom">Menu Item 3</a></li>' +         '</ul>' +         '</div>'); } 
like image 689
Luke Shaheen Avatar asked Feb 05 '12 19:02

Luke Shaheen


1 Answers

I've been trying to tackle this problem for a few days now and I've gotten really close to the solution. I use the following HTML:

<body>     <div data-role="page" id="page">          <div data-role="header">             <h1 id="title">Title</h1>         </div><!-- /header -->          <div data-role="content" id="content">               <p>Loading...</p>         </div><!-- /content -->          <div data-role="footer" id="footer" data-position="fixed">             <div data-role="navbar" id="navbar">             </div>         </div><!-- /footer -->     </div><!-- /page --> </body> 

And I've created the following functions to load the menu/content using ajax:

$(document).on('pageinit', "#page", function() {     // for example: displayFooter();     loadContent("main");     loadMenu("default"); });  function loadContent(location) {     return $('#content').load("views/content/"+location+".html", function() {          $(this).trigger('create');     }); } function loadMenu(location) {     $("#menu").remove();     $("#navbar").remove();      $("#footer").html('<div data-role="navbar" id="navbar">');     $("#navbar").html('<ul id="menu"></ul>');      $("#menu").load("views/menus/"+location+".html", function() { $("#menu").parent().navbar(); });      return true; } 

The .trigger('create'); and .navbar(); are the methods required to keep the JQM styling correct, however, there's still one little bug. The position of the menu (which is set using css top: ...px) is sometimes not correct and moves itself to the correct position after the first tap. Really close though!

Edit: By setting #footer to position: fixed; the minor bug I mentioned above is gone. Also, it's easy to make a method that calculates the top (in px) for the #footer if the position caused by the top value by JQM is incorrect.

like image 126
sougonde Avatar answered Oct 11 '22 11:10

sougonde