我用于提取一些数据以在列表视图中使用的代码有 2 个奇怪的问题。这是我的javascript:
function getOrders(status, url) {
$(function () {
//check if url from pagination
if (!url) {
url = api_url + '/orders/?callback=?&status=' + status;
} else {
url = root_url + url + '&callback=?';
}
$.mobile.loading('show');
$.getJSON(url, null, function (d) {
//declare a variable with which to build our output (it's best to buffer output and only do one append at the end since DOM manipulation is CPU expensive)
var output = '';
//iterate through the data (we could also get rid of the jQuery here by using `for (key in data) {
$.each(d.objects, function (index, value) {
output += '<li><a id="' + value.reference + '" href="view_order.html" class="view_order"><h3>' + value.reference + ' - ' + value.client.company + '</h3><p>' + value.order_date + ' ' + value.user.username + '</p></a><a href="asd" data-icon="gear" data-iconpos="notext"></a></li>';
});
$('#orders_list').html(output).listview('refresh');
//if paginated, update next button
if (d.meta.next) {
$("#id_ordersNext").attr('href', d.meta.next);
$("#id_ordersNext").show();
} else {
$("#id_ordersNext").hide();
}
if (d.meta.previous) {
$("#id_ordersPrevious").attr('href', d.meta.previous);
$("#id_ordersPrevious").show();
} else {
$("#id_ordersPrevious").hide();
}
$("#id_ordersTotal").html(d.meta.total_count);
$.mobile.loading('hide');
});
});
}
$(function () {
//bind the nav
$(".order_nav").die();
$(".order_nav").live('click', function () {
$(".order_nav").each(function () {
$(this).removeClass('ui-btn-active');
});
$(this).addClass('ui-btn-active');
getOrders($(this).attr('href'));
return false;
});
//bind the view order
$(".view_order").die();
$(".view_order").live('click', function () {
//save var
window.viewOrderReference = $(this).attr('id');
$.mobile.changePage("view_order.html");
});
$("#id_ordersNext,#id_ordersPrevious").die();
$("#id_ordersNext,#id_ordersPrevious").live('click', function () {
getOrders(null, $(this).attr('href'));
return false;
});
//default view
getOrders('Order Placed');
});
这是我用于通过 JQMobile 加载的页面的 html:
<div data-role="page" data-needs-auth='true'>
<script src="js/list_orders.js"></script>
<div class="headerDiv" data-role='header' data-theme='b'><a href="index.html" data-icon="home">Home</a>
<h1>Jubilee Distributors</h1>
<a href="login.html" class="logged_in" data-icon="home">Login</a></div>
<div data-role='navbar'>
<ul>
<li><a href="Order Placed" class='ui-btn-active order_nav' data-ajax="false">Placed</a></li>
<li><a href="Order Picked" class='order_nav' data-ajax="false">Picked</a></li>
<li><a href="Order Delivered" class='order_nav' data-ajax="false">Delivered</a></li>
</ul>
</div>
<div data-role="content">
<ul data-role='listview' id="orders_list" data-filter="true"><li>No records found</li></ul>
<p><a href='#' id='id_ordersPrevious' style='display:none;' data-role="button" data-icon="arrow-l" data-iconpos="notext" data-inline="true">Previous</a> <span id='id_ordersTotal' class='record-count'></span> records found <a href='#' id='id_ordersNext' style='display:none;' data-role="button" data-icon="arrow-r" data-iconpos="notext" data-inline="true">Previous</a>
</p>
</div>
<div id='footerDiv' data-role="footer"></div>
</div>
这一切在桌面上的任何浏览器中都可以正常工作,但是当我在 Android 设备上运行它时,会发生 2 件事,或者说不会。
- $(function() - getOrders('Order Placed') 中的最后一行似乎没有执行,或者如果执行了,它不会使用返回的结果更新列表。如果我单击第一个带有“已下订单”它没有问题。
- addClass 实际上并没有添加类。
就像我说的,这在任何桌面浏览器中都可以正常工作,但在 Android 设备上却不行。
有任何想法吗?
编辑:修复了第二个问题,但是第一个问题仍然存在。如果我导航到页面,然后离开它,然后再返回,它就可以工作。