Use jQuery Mobile to implement the Mobile news browser (chapter 2) (1)

Source: Internet
Author: User

In the previous article, I have discussed the program structure and page layout, and briefly introduced some jQuery Mobile usage skills. In this article, I will continue to design the news browser for our web applications.

Program startup

Now let's look at the startup of the program. When the program is started, the page of the news category list is displayed to the user. To make this page not empty, we need to note which types of interest the user has selected before. To achieve this goal, we use a jQuery plug-in DST. js plugin to save the news categories selected by users in the localStorage of html5. If a user removes a category, it will also be removed from the local storage area in the browser (note that this function can be implemented only in browsers that support HTML5 standards ). When loading a page, we can use $ (document) in jQuery ). the ready () function obtains saved news categories and processes the latest news under each news category one by one. The Code is as follows:

 
 
  1. Var COMMA = ',';
  2. Var COOKIE_NAME = 'News ';
  3. // NumNewsToRestore saves the number of news categories selected by the user
  4. Var numNewsToRestore = 0;
  5. Var storedNewsArr;
  6. ...
  7. $ (Document). ready (function (){
  8. ShowProgress ();
  9. Var storedNewsTxt = $. DSt. get (COOKIE_NAME );
  10. If (storedNewsTxt! = Null & storedNewsTxt. length> 0 ){
  11. StoredNewsArr = storedNewsTxt. split (COMMA );
  12. } Else {
  13. StoredNewsArr = new Array ();
  14. }
  15. NumNewsToRestore = storedNewsArr. length;
  16. Restore ();
  17. });

In the above Code, after you select a news classification, the selected news is connected by commas (,) and stored in the storedNewsArr variable, for example, if you select "Top Stories" and "Politics", the localStorage region contains the string "topstories, politics". Then, we call the restore () function as follows:

 
 
  1. function restore(){ 
  2.   if(numNewsToRestore > 0){ 
  3.      getNews(storedNewsArr[--numNewsToRestore],restoreNews); 
  4.   }else{ 
  5.      showCategories(); 
  6.   } 

In the restore () method, if the news category directory does not exist, we only need to display an empty news page. If a news category exists, call the getNews () method and pass the latest category as a parameter. Another parameter of the getNews () method is restoreNews. Next, let's take a look at the getNews () and restoreNews () methods:

 
 
  1. var NEWS_URI = 'bridge.php?fwd=http://rss.news.yahoo.com/rss/'; 
  2. ... 
  3. function getNews(varCat,handler){ 
  4.    var varURI = NEWS_URI + varCat; 
  5.    $.ajax({type: GET, dataType: XML, url: varURI, success: handler}); 
  6.    return false; 
  7. ... 
  8. function restoreNews(xml){ 
  9.    populateSingleNews(xml); 
  10.    restore(); 

The getNews () method provides the address varURI that initiates a request to ajax. The uri is in the following format: bridge. php? Fwd = http://rss.news.yahoo.com/rss/+ varCat, where varCat is the selected news directory, such as bridge. php? Fwd = http://rss.news.yahoo.com/rss/topstories. The callback function returned by ajax is restoreNews (). In restoreNews (), a custom method populateSingleNews () is called. This method will be parsed later. At last, the restore method was re-called to form a recursive call in the following order:

 
 
  1. restore -> getNews -> restoreNews -> restore -> ...   

The numNewsToRestore parameter indicates the number of news categories selected. In the ready method of jQuery, the plug-in DST is used first. after the js plug-in reads the cookie, it forms a string array storedNewsArr, and then calls the restore method to display the last element (that is, the category selected by the latest user) in storedNewsArr) pass in the getNews method to obtain the news under this category, and use the restoreNews () method to process the content returned by the ajax callback, and finally call restore () method to process the last 2nd news categories in the storedNewsArr array, and so on.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.