5 <meta name="viewport" content="width=device-width">
6 <title>changePage JSON Sample</title>
7 <link rel="stylesheet" href="../../../css/themes/default/">
8 <script src="../../../js/jquery.js"></script>
9 <script src="../../../js/"></script>
10 <script type="text/javascript">
12 // Load the JSON data for a specific category, based on
13 // the URL passed in. Generate markup for the items in the
14 // category, inject it into an embedded page, and then make
15 // that page the current active page.
16 function showCategory( url, options )
18 $.getJSON( url, function( category ) {
20 // Get the page we are going to dump our content into.
21 var $page = $( "#category-items" ),
23 // Get the header for the page.
24 $header = $page.children( ":jqmData(role=header)" ),
26 // Get the content area element for the page.
27 $content = $page.children( ":jqmData(role=content)" ),
29 // The markup we are going to inject into the content
31 markup = "<p>" + category.description + "</p><ul data-role='listview' data-inset='true'>",
33 // The array of items for this category.
34 cItems = category.items,
36 // The number of items in the category.
37 numItems = cItems.length;
39 // Generate a list item for each item in the category
40 // and add it to our markup.
41 for ( var i = 0; i < numItems; i++ ) {
42 markup += "<li>" + cItems[i].name + "</li>";
46 // Find the h1 element in our header and inject the name of
47 // the category into it.
48 $header.find( "h1" ).html( category.name );
50 // Inject the category items markup into the content element.
51 $content.html( markup );
53 // Pages are lazily enhanced. We call page() on the page
54 // element to make sure it is always enhanced before we
55 // attempt to enhance the listview markup we just injected.
56 // Subsequent calls to page() are ignored since a page/widget
57 // can only be enhanced once.
60 // Enhance the listview we just injected.
61 $content.find( ":jqmData(role=listview)" ).listview();
63 // We don't want the data-url of the page we just modified
64 // to be the url that shows up in the browser's location field,
65 // so set the dataUrl option to the URL for the category
67 options.dataUrl = url;
69 // Now call changePage() and tell it to switch to
70 // the page we just modified.
71 $.mobile.changePage( $page, options );
77 // Listen for any attempts to call changePage().
78 $(document).bind( "pagebeforechange", function( e, data ) {
79 // We only want to handle changePage() calls where the caller is
80 // asking us to load a page by URL.
81 if ( typeof data.toPage === "string" ) {
82 // We are being asked to load a page by URL, but we only
83 // want to handle URLs that request the data for a specific
85 var u = $.mobile.path.parseUrl( data.toPage );
86 if ( u.pathname.search("category.php") !== -1 ) {
87 // We're being asked to display the items for a specific category.
88 // Call our internal method that builds the content for the category
89 // on the fly based on our in-memory category data structure.
90 showCategory( u.href, data.options );
92 // Make sure to tell changePage() we've handled this call so it doesn't
93 // have to do anything.
103 <div data-role="page">
104 <div data-role="header"><h1>Categories</h1></div>
105 <div data-role="content">
106 <h2>Select a Category Below:</h2>
107 <ul data-role="listview" data-inset="true">
108 <li><a href="category.php?id=animals">Animals</a></li>
109 <li><a href="category.php?id=colors">Colors</a></li>
110 <li><a href="category.php?id=vehicles">Vehicles</a></li>
114 <div id="category-items" data-role="page" data-add-back-btn="true">
115 <div data-role="header"><h1></h1></div>
116 <div data-role="content"></div>