Authored by xuqi

search local store

@@ -10,18 +10,20 @@ var $input = $('#search-input input'); @@ -10,18 +10,20 @@ var $input = $('#search-input input');
10 10
11 var $clear = $('#search-input .clear-input'); 11 var $clear = $('#search-input .clear-input');
12 12
  13 +var $form = $('#search-form');
  14 +
13 var $history = $('.history'); 15 var $history = $('.history');
14 16
15 -$('#clear-history').bind('tap', function() {  
16 - $.ajax({  
17 - type: 'POST',  
18 - url: '/search/clearHistory',  
19 - success: function(data) {  
20 - if (data.code === 200) {  
21 - $history.html(''); //clear search history items  
22 - }  
23 - }  
24 - }); 17 +var $clearHistory = $('#clear-history');
  18 +
  19 +var writeSearch = require('./write-search');
  20 +
  21 +var ranToken = writeSearch.getRanToken();
  22 +
  23 +$clearHistory.bind('tap', function() {
  24 + localStorage.removeItem('historys');
  25 +
  26 + $history.html('');
25 }); 27 });
26 28
27 $input.bind('input', function() { 29 $input.bind('input', function() {
@@ -34,4 +36,33 @@ $input.bind('input', function() { @@ -34,4 +36,33 @@ $input.bind('input', function() {
34 36
35 $clear.bind('tap', function() { 37 $clear.bind('tap', function() {
36 $input.val('').trigger('input'); 38 $input.val('').trigger('input');
37 -});  
  39 +});
  40 +
  41 +//初始化历史搜索的内容
  42 +(function() {
  43 + var html = '',
  44 + history,
  45 + historys, i;
  46 +
  47 + if (localStorage) {
  48 + historys = localStorage.getItem('historys');
  49 +
  50 + if (historys && historys.length > 0) {
  51 + historys = historys.split(ranToken);
  52 + for (i = 0; i < historys.length; i++) {
  53 + history = historys[i];
  54 +
  55 + if (history === '') {
  56 + continue;
  57 + }
  58 +
  59 + html += '<li><a href="?query=' + history + '">' + history + '</li>';
  60 + }
  61 +
  62 + $history.html(html);
  63 + $clearHistory.removeClass('hide');
  64 + }
  65 + }
  66 +}());
  67 +
  68 +writeSearch.bindWirteLocal($form);
  1 +/**
  2 + * 将搜索结果存localStorage
  3 + * @author: xuqi<qi.xu@yoho.cn>
  4 + * @date: 2015/10/29
  5 + */
  6 +
  7 +var ranToken = ' ??++ ';
  8 +
  9 +//获取分隔符
  10 +function getRanToken() {
  11 + return ranToken;
  12 +}
  13 +
  14 +//绑定提交前的存local操作
  15 +function bindWirteLocal($form) {
  16 + $form.on('submit', function() {
  17 + var query = this.query.value,
  18 + historys;
  19 +
  20 + if (localStorage) {
  21 + historys = localStorage.getItem('historys');
  22 +
  23 + historys = historys ? historys : '';
  24 +
  25 + if (historys.indexOf(ranToken + query + ranToken) > -1) {
  26 + return;
  27 + }
  28 +
  29 + if (historys === '') {
  30 + query = ranToken + query;
  31 + }
  32 +
  33 + historys += query + ranToken;
  34 +
  35 + localStorage.setItem('historys', historys);
  36 + }
  37 + });
  38 +}
  39 +
  40 +exports.getRanToken = getRanToken;
  41 +
  42 +exports.bindWirteLocal = bindWirteLocal;
@@ -13,6 +13,8 @@ var $brandHeader = $('#brand-header'), @@ -13,6 +13,8 @@ var $brandHeader = $('#brand-header'),
13 13
14 var filter = require('../plugin/filter'); 14 var filter = require('../plugin/filter');
15 15
  16 +var writeSearch = require('../index/write-search');
  17 +
16 var tip = require('../plugin/tip'); 18 var tip = require('../plugin/tip');
17 var loading = require('../plugin/loading'); 19 var loading = require('../plugin/loading');
18 20
@@ -210,6 +212,8 @@ lazyLoad($('.lazy')); @@ -210,6 +212,8 @@ lazyLoad($('.lazy'));
210 212
211 filter.registerCbFn(search); 213 filter.registerCbFn(search);
212 214
  215 +writeSearch.bindWirteLocal($('#search-form'));
  216 +
213 //导航栏点击逻辑说明: 217 //导航栏点击逻辑说明:
214 //1.点击非active项时切换active状态 218 //1.点击非active项时切换active状态
215 //2.价格和折扣active状态时继续点击切换排序 219 //2.价格和折扣active状态时继续点击切换排序
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <div class="search-page yoho-page"> 2 <div class="search-page yoho-page">
3 {{# search}} 3 {{# search}}
4 <div id="search-input" class="search-input"> 4 <div id="search-input" class="search-input">
5 - <form action="/product/list/index" method="get"> 5 + <form id="search-form" action={{url}} method="get">
6 <i class="search-icon iconfont">&#xe60f;</i> 6 <i class="search-icon iconfont">&#xe60f;</i>
7 <input type="text" placeholder="搜索商品" name="query"> 7 <input type="text" placeholder="搜索商品" name="query">
8 <i class="clear-input iconfont hide">&#xe626;</i> 8 <i class="clear-input iconfont hide">&#xe626;</i>
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 </form> 10 </form>
11 </div> 11 </div>
12 <div class="search-items"> 12 <div class="search-items">
13 - <div class="hot-search"> 13 + <div class="hot-search hide">
14 <h3>热门搜索</h3> 14 <h3>热门搜索</h3>
15 <ul class="hot clearfix"> 15 <ul class="hot clearfix">
16 {{# hot}} 16 {{# hot}}
@@ -22,15 +22,9 @@ @@ -22,15 +22,9 @@
22 </div> 22 </div>
23 <div class="history-search"> 23 <div class="history-search">
24 <h3>历史搜索</h3> 24 <h3>历史搜索</h3>
25 - <ul class="history clearfix">  
26 - {{# history}}  
27 - <li>  
28 - <a href={{url}}>{{name}}</a>  
29 - </li>  
30 - {{/ history}}  
31 - </ul> 25 + <ul class="history clearfix"></ul>
32 </div> 26 </div>
33 - <button id="clear-history" class="clear-history">清空搜索历史</button> 27 + <button id="clear-history" class="clear-history hide">清空搜索历史</button>
34 </div> 28 </div>
35 {{/ search}} 29 {{/ search}}
36 </div> 30 </div>
1 {{# goodList}} 1 {{# goodList}}
2 {{# search}} 2 {{# search}}
3 <div id="search-input" class="search-input"> 3 <div id="search-input" class="search-input">
4 - <form action={{url}} method="get"> 4 + <form id="search-form" action={{url}} method="get">
5 <i class="search-icon iconfont">&#xe60f;</i> 5 <i class="search-icon iconfont">&#xe60f;</i>
6 <input type="text" value={{default}} name="query"> 6 <input type="text" value={{default}} name="query">
7 <i class="clear-input iconfont hide">&#xe61a;</i> 7 <i class="clear-input iconfont hide">&#xe61a;</i>