//!* package adds search record function (Newrecord:str, gets history when parameter is empty; Num: Record number, default is 12;)function Addhisrecord (NewRecord, num) {num= num | | A; //to get locally stored records varHisrecord_str = Window.localStorage.getItem ('Hisrecord_str'); //When the obtained value is NULL, create an empty array to store it in if(typeofHisrecord_str = ='Object'){ varHisrecord = []; Hisrecord_str= Hisrecord.join ('|'); Window.localStorage.setItem ('Hisrecord_str', HISRECORD_STR); } //Converting to arraysHisrecord = Hisrecord_str.split ('|'); //Empty Array When Hisrecord_str is an empty string if( !hisrecord_str) {Hisrecord.pop (); } //when the argument is not empty, determine: if(NewRecord) {//1. When the incoming value does not exist in the array, the new record is added to the first; if(Hisrecord.indexof (newrecord) = =-1) {hisrecord.unshift (NewRecord); //When the record is less than NUM, add to the first, and when it is greater than or equal to NUM, add to the first and delete the bottom if(Hisrecord.length >num) {Hisrecord.pop (); } }Else{ //2. When the incoming value is present in the array, the record of the corresponding position is deleted and the new record is added to the first; varIndexrecord =Hisrecord.indexof (NewRecord); Hisrecord.splice (Indexrecord,1); Hisrecord.unshift (NewRecord); } //Convert to string stored locallyHisrecord_str = Hisrecord.join ('|'); Window.localStorage.setItem ('Hisrecord_str', HISRECORD_STR); }Else{//returns the acquired history when the argument is empty, Type:array; returnHisrecord; } }
//!* Package Delete search record function (index:0,1,2,..., ' all ';)function Removehisrecord (index) {//to get locally stored records varHisrecord_str = Window.localStorage.getItem ('Hisrecord_str'); //Converting to arrays varHisrecord = Hisrecord_str.split ('|'); //Empty Array When Hisrecord_str is an empty string if( !hisrecord_str) {Hisrecord.pop (); }Else if(Index = =' All') {Hisrecord.splice (0, hisrecord.length); }Else{hisrecord.splice (index,1); } //store and render processed data to a pageHisrecord_str = Hisrecord.join ('|'); Window.localStorage.setItem ('Hisrecord_str', HISRECORD_STR); Hideorshowhisrecord (HISRECORD_STR);//Hide History section//template_data (Hisrecord, ' #search-item-tmpl3 ', ' #searchBox3 ');}
//!* Package Query and render search record function when entering pagefunction Hisrecord () {varHisrecord =Addhisrecord (); varHisrecord_str =Hisrecord.join (); //rendering when records exist if(HISRECORD_STR) {template_data (Hisrecord,'#search-item-tmpl1','#searchBox1'); } //Hideorshowhisrecord (HISRECORD_STR);//control display and hide}
// render data to the page according to the template (data: What to render, Template: Templates, Box: boxes to be rendered ) // need to introduce dot.js template engine function Template_data (data, template, box) { var searchitemtmpl = dot.template ($ (template). html ()); $ (box). HTML (Searchitemtmpl (data)); }
Work on search page search record function encapsulation (store to local)