Framework7 get data from Ajax and refresh

Source: Internet
Author: User
Tags json


Reprinted from: http://www.680.com/Web/1609/webedit-43324.html



A simple introduction to the use of the Template7 template page. The list data in the template page was defined when the FRAMEWORK7 was initialized.






However, the actual development of the page data is not always constant. Instead, the data is fetched from an external or remote server through an AJAX request.






Let's say we have an external data to load: News.json






{






"title": "Latest News",






"News": [






{






"title": "Welcome to visit Hangge.com",






"Date": "08-20"






},






{






"title": "Framework7 page cache Settings",






"Date": "08-19"






},






{






"title": "Olympic athletes won the gold medal",






"Date": "08-19"






}






]






}






There are two ways to populate the Template7 page with the data you get.






Method 1:






In the Framework7 initialization preprocess method, the Load list page is captured for this routed event. The data is obtained through AJAX, and the data gets populated with the template before continuing.






Initialize App






var myApp = new Framework7 ({






Precompiletemplates:true,






Template7pages:true,//pages enable Template7






Template7data: {






},






Preprocess:function (content, URL, next) {






Judging if it's jump to the list page






if (Url.indexof ("list.html") >=0) {






Get the data first






$$.getjson ("Data/news.json", function (data) {






Console.log (data);






Template compilation






var compiledtemplate = template7.compile (content);






Template Data loading






Next (compiledtemplate (data));






});






}else{






Other pages follow normal process.






Next (content);






}






}






});






Method 2:






The same is true of the Preprocess method, which captures the routed event of the Load List page first. When data is obtained through AJAX, the obtained data is placed in the Template7 context data. Then continue loading the page.






Initialize App






var myApp = new Framework7 ({






Precompiletemplates:true,






Template7pages:true,//pages enable Template7






Template7data: {






},






Preprocess:function (content, URL, next) {






Judging if it's jump to the list page






if (Url.indexof ("list.html") >=0) {






Get the data first






$$.getjson ("Data/news.json", function (data) {






Console.log (data);






Setting context Data






template7.data["page:list"] = data;






Page continue to jump






Next (content);






});






}else{






Other pages follow normal process.






Next (content);






}






}






});






Method 3:






Do not jump directly from the link. Instead, the data is loaded in the linked click event, and the data is loaded into the TEMPLATE7 context data. Finally, load the list page again.






(1) Home jump link href set to #






<a href= "#" class= "open-list" > Open list Page </a>






(2) JS related code






Initialize App






var myApp = new Framework7 ({






Precompiletemplates:true,


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.