Somewhat related to this i guess,
Lets say i have a list of categories that are dynamic.
- <ul data-role="listview">
- <li><a data-id="504" href="#catalog-504">Foobar</a></li>
- <li><a data-id="505" href="#catalog-505">Foobar</a></li>
- <li><a data-id="506" href="#catalog-506">Foobar</a></li>
- </ul>
I have a click event bound to each of those anchor tags that has e.preventDefault() and e.stopPropagation(). Also within that click event, i'm cloning the current page, replacing the ul with a new empty ul, populating it, then initializing the listview. After that, i perform a $.mobile.changepage to the page i just created and unbind the click event so that future clicks on that anchor tag will not rebuild the page (since it now exists in the DOM).
The problem is that when you click on the anchor, jqm creates a new empty page since the requested page doesn't exist yet, then, i populate the page, and transition to it, resulting in a double transition. How can i prevent jqm from creating the new empty page, and have it simply wait until i build the page and manually transition to it?
- $('[id^="catalog"]').live("pageinit", function() {
- var id = this.id.split("-")[1],
- $page = $(this),
- $pageul = $page.find("ul");
- id = id ? id : 0;
- $.getJSON("ajax/getCategories.cfm?category_id=" + id).done(function(data) {
- for (var i = 0; i < data.recordcount; i++) {
- $pageul.append(
- $("<li></li>").html("<a data-itemtype='" + (data.data.isproduct[i] ? "product" : "catalog") + "' data-id='" + data.data.id[i] + "' href='#" + (data.data.isproduct[i] ? "product-" : "catalog-") + data.data.id[i] + "'>" + data.data.name[i] + "</a>"));
- }
- $pageul.listview("refresh");
- $pageul.find("li a").bind("click", function(e) {
- e.preventDefault();
- e.stopPropagation();
- var id = $(this).data("id"),
- itemtype = $(this).data("itemtype"),
- pageId = itemtype + "-" + id,clone;
- if ($('#' + pageId).length === 0) {
- clone = $page.clone();
- clone.appendTo("body");
- if (itemtype === "product") {
- clone.find("ul").remove();
- clone.find(".content").before("<div class='header-image'></div>");
- }
- else {
- clone.find("ul").replaceWith("<ul data-role='listview'></ul>");
- }
- clone.attr("id", pageId);
- }
- $.mobile.changePage("#" + pageId);
- $(this).unbind("click");
- });
- });
- });
PS: i know i'm still using .live() .bind() and .unbind(), i had to keep it compatible with older versions of jquery for now just in case.
-- Kevin