write-search.js 1.78 KB
/**
 * 将搜索结果存localStorage
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/29
 */

var ranToken = ' ??++ ';
var $ = require('jquery');

//获取分隔符
function getRanToken() {
    return ranToken;
}

/**
 * 增加单条记录
 */
function addHistory(brandName, brandId, brandDomain) {
    $.ajax({
        type: 'GET',
        url: '/brands/addBrandSearch',
        data: 'brandName=' + brandName + '&brandId=' + brandId + '&brandDomain=' + brandDomain,
        success: function(data) {
        },
        error: function() {
        }
    });
}

//绑定提交前的存local操作
function bindWirteLocal($brandList) {
    $brandList.on('click', 'p , li', function(e) {
        var brandName, brandId, brandDomain, url;
        var a = $(this).find('a');
        var query = a.attr('brandName') + a.attr('href'),
            historys;

        brandName = a.attr('brandName');

        brandId = a.attr('brandId');

        brandDomain = a.attr('brandDomain');

        url = a.attr('href');

        e.preventDefault();

        if ($('.net-search').length > 0) {
            addHistory(brandName, brandId, brandDomain);
            location.href = url;
            return;
        }

        if (localStorage) {
            historys = localStorage.getItem('historys-brand');

            historys = historys ? historys : '';

            if (historys.indexOf(ranToken + query + ranToken) > -1) {
                location.href = url;
                return;
            }

            if (historys === '') {
                query = ranToken + query;
            }

            historys += query + ranToken;

            localStorage.setItem('historys-brand', historys);
        }



        location.href = url;
    });
}


exports.getRanToken = getRanToken;

exports.bindWirteLocal = bindWirteLocal;