2010-08-17 46 views
0

我简化了这个问题jqtouch移动应用AJAX加载问题

SIMPLIFIED: jqtouch mobile app ajax loading issue

,但我离开这个职位的详细信息。谢谢。

我有我作为一个操场学习使用RSS阅读器应用程序。目前的化身是学习jqtouch /移动应用程序的东西。我在动态加载帖子时遇到了问题,因为jqtouch接管了'a'并对它做了一些操作。

这里的一般过程:

- Page loads general html (this is very basic, a couple of empty divs 
    between the body tags) and a link to the reader 
- when the document is ready, the first thing that fires is 
    an ajax call that gets a list of the blogs I'm following that 
    have unread posts and appends it to the proper divs 
- when I click the link to the reader, all the blog titles are there 
- I click a blog title and it is to load all of the unread posts 

的问题是,jqtouch &人工授精两种看似强迫你使用一个“A”标签页/卡之间去,我要动态加载的博客文章作为他们是需要的,因为速度(最终我将实施清单和localStorage)。对于我想要做的事情,使用JSON加载它非常重要。我想尽可能快速和高效地做到这一点。我宁愿将一个点击绑定到一个div上,而不是一个“a”,除了跳到新的页面/卡片之外,我还可以通过单击来做更多的事情。

这里的HTML部分:

<body> 
<div id="home"> 
<h1>TERSE</h1> 
<ul> 
    <li><a href="#feeds">teRSSe</a></li> 
</ul> 
</div> 
<div id="feeds"> 
Loading, please wait ... 
</div> 
</body> 

这里是被解雇时,页面加载:

$(document).ready(function() 
{ 
// onload 
$.ajax({ 
    type: 'GET', 
    url: '/ajax/feeds', 
    dataType: 'json', 
    success: function(data) 
    { 
     append_feeds(data); 
    }, 
    async: true 
}); 
    // ... 

这是在饲料标题添加功能:

function append_feeds(data) 
{ 
var feeds = ''; 
    // loop 1, add the links that will be shown in the main feeds block 
for (i=0; i<data.length; i++) 
{ 
    var this_data = data[i];  
    if (this_data.unread == 0) 
    { 
     continue; 
    } 

    feeds = feeds+'<li title="'+this_data.title+'" class="feed-list" id="'+this_data.id+'">\n'; 
    feeds = feeds+ '<a id="'+this_data.hid+'" href="#feed-'+this_data.id+'">'+this_data.title+' &nbsp; ('+this_data.unread+' unread)</a>\n'; 
    feeds = feeds+'</li>\n'; 
} 

$('#feeds').html('<div id="toolbar">\n<h1>RSS</h1>\n<a class="button back" href="#">Back</a>\n</div>\n<ul title="RSS" id="feedul">\n'+feeds+'</ul>\n'); 

    // loop 2; 2nd time around to create pages/cards for each blog 
    // this is where the individual posts will load when the blog 
    // title is clicked 
for (i=0; i<data.length; i++) 
{ 
    var this_data = data[i];  
    if (this_data.unread == 0) 
    { 
     continue; 
    } 

    var A = '<div id="feed-'+this_data.id+'">\n'; 
    A = A + ' <h1>'+this_data.title+'</h1>\n'; 
    A = A + ' <ul id="'+this_data.hid+'" title="'+this_data.title+'">\n'; 
    A = A + ' <li id="load-'+this_data.id+'" class="load">loading '+this_data.title+' ...</li>\n'; 
    A = A + ' </ul>\n'; 
    A = A + '</div>\n'; 
    $('body').append(A); 
} 
} 

到目前为止,我们已经有了基本的html,而append_feeds()用一列标题+ num_unread填充了一大堆div/uls我正在关注的博客。另外,我还设置了其他divs作为链接的目标,每个博客一个。这些是“页面”或“卡片”。但是,当我点击博客的标题时,我想要使用ajax/json抓取单个博客的所有帖子,然后更新正确的feed的div,并加载页面/卡片(基本上是jqtouch和iui拉出与href =“”中的哈希ID匹配的div)。

我试过绑定/活到点击/点击事件,但JQT/IUI seemlingly接听来电。我会完全跳过“a”标签,只需将点击绑定到“li.post”或其他东西,但jqtouch不会将其识别为点击。它似乎只想让我使用实际的链接作为点击事件。

回答

0

我只是通过你会通过什么去了。您需要打开该XML为JSON对象,将被编号[0],[1]等

这个jQuery插件工程和岩石http://www.fyneworks.com/jquery/xml-to-json/ 添加一个JS把你的应用程序。然后,您的解析XML函数将您想要的XML节点(如频道内的项目节点)转换为JSON对象,然后对这些项目进行编号。

所以,看我怎么然后清空项目的当前列表,建立项目清单,并添加自定义点击功能列表项与一类“太阳”(我爱jQuery的)的。该函数然后将它的父节点标题和desc添加到需要它的div。 href将把它推到新的jqtouch DIV中,它将处理所有的细节信息。酷'呃?如果这有效,请投我一票。它为我做了,就像一个魅力。

<ul id="feedlist"></ul>  
<div id="titleDiv"></div>  
<div id="descDiv"></div> 

    function parseXml(xml) 
    { 
     $('#feedList').html(''); 
     var rss = $.xml2json(xml); 
     $.each(rss.channel.item, function(i, item) 
     { 
     $('#feedList').empty().append('<li class=sun'+i><a href=#contentDiv>'+item.title+'</a></li>'); 
     $('.sun'+i).click(function() { 
      $('#titleDiv').empty().append(item.title);     
      $('#descDiv').empty().append(item.description); 
      }); 
     }); 
    };