2011-12-17 282 views
1

我想使用JSON将一些外部数据加载到JQTouch应用程序中。我的问题是我不知道要使用什么j以及在哪里放置它。使用JSON为JQTouch加载外部数据

我的html非常简单 - 几个div在应用程序中的页面。

<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> google.load("jquery", "1.4.2"); </script> 
    <script type="text/javascript" src="jqtouch/jqtouch.min.js" charset="utf-8"></script> 
    <script type="text/javascript"> $.jQTouch(); </script> 
    <script src="javascripts/cricket.js" type="text/javascript"></script> 
    <style type="text/css" media="screen">@import "jqtouch/jqtouch.min.css";</style> 
    <style type="text/css" media="screen">@import "themes/apple/theme.css";</style> 
    <title>Cricket</title> 

</head> 
<body> 

<!-- "Main Menu" --> 
<div id="home" class="current"> 
<div class="toolbar"> 
    <h1>Cricket</h1> 
</div> 

<ul class="rounded"> 
<li class="arrow"><a href="#clubs">Clubs</a></li> 
    <li class="arrow"><a href="#fixtures">Fixtures</a></li> 
<li class="arrow"><a href="#twitter">Twitter</a></li> 
</ul> 

<ul class="rounded"> 
<li class="arrow"><a href="#about" class="slideup">About</a></li> 
</ul> 


</div> 


<!-- "Clubs" - Will contain the list of clubs --> 
<div id="clubs"> 

<div class="toolbar"> 
     <h1>Clubs</h1> 
     <a class="button back" href="#">Back</a> 
</div> 

<div id="clubslist"> 

    Loading.... 

</div> 


</div> 

<!-- "Fixtures" - Will contain the list of fixtures --> 
<div id="fixtures"> 
<div class="toolbar"> 
    <h1>Fixtures</h1> 
    <a class="button back" href="#">Back</a> 
</div> 

<ul class="rounded"> 
    <li>Loading....</li> 
</ul> 

</div> 

<!-- "Twitter" - Will contain the tweets --> 
<div id="twitter"> 
<div class="toolbar"> 
    <h1>Twitter</h1> 
    <a class="button back" href="#">Back</a> 
</div> 

<ul class="rounded"> 
    <li>Loading tweets....</li> 
</ul> 

</div> 


<!-- "About" - Will contain info about the app/contact --> 
<div id="about"> 
<div class="toolbar"> 
    <h1>About</h1> 
    <a href="#" class="back">Back</a> 
</div> 
just some text 
</div> 

那么这就是我认为我需要使用加载外部数据(来自clubs.php)的脚本。

<script type="text/javascript"> 
      $.getJSON('http://www.mydomain.com/clubs.php', function(data) { 

    $.each(data, function(i,item){ 
       $('clubslist').append('<li>' + item.club_name + '</li>'); 

      }); 
    }); 

    </script> 

我的问题是我需要把它放在哪里以及它如何被触发? 我想我需要把它放在一个单独的JS文件中(我已经有一个叫做cricket.js的链接到html文件的头部)。那么我假设我需要一些东西来触发JS运行......比如PageAnimationEnd或类似的东西?

对于它的价值,这是什么clubs.php返回

{"posts":[{"post":{"club_id":"1","club_name":"ABC Cricket Club","club_postcode":"AB12 3DE"}},{"post":{"club_id":"2","club_name":"Beston Cricket Club","club_postcode":"NG1 9XY"}}]} 

我理想的情况是,当用户点击菜单页面上的“俱乐部”链接,这将激活页面动画哪里用户然后看到'正在加载'。在后台,Javascript被激发,从clubs.php中检索数据,然后将其附加到#clublist div中。

我觉得我很接近,但失去了最后的画龙点睛。所有建议表示赞赏 Tom

回答

0

在我的页面中,我执行以下操作,在此示例中,我从后端脚本中拉出一个表。

function prevOrders() 
    { 
    $("#chkPrevOrd").html('Please wait') 
    $.ajax({ 
     type: "POST", 
     url: "http://"+document.domain+"/store.php", 
     data: { method: "prevOrders"},  
     dataType: "json", 
     timeout: ajaxTimeout, 
     success: function(data){ 
     if (data.prevOrders) 
      { 
      $("#prevOrderStatus").html(data.prevOrders); 
      $("#chkPrevOrd").html('Update')  
      }    
      }, 
     error: function() { 
     alert('This is taking too long. You could try again now, or wait and try again later.'); 

      } 
});  
    } 

#prevOrderStatus此页面上有一个DIV,#chckPrevOrd只是一个按钮,让用户知道发生了什么。如果#chkPrevOrd实际上位于不同的页面上,您可以将JQT.Goto('cointainer div for #chkPrevOrd')转到此页面。

+0

非常好,谢谢你,我会给它一个。我是否正确地认为你实际上在php文件中格式化表格html,然后将完成的html表格传递给应用程序? – Hardball 2011-12-20 08:45:37