address.js 1.93 KB
/**
 * 地址管理
 * @author: bikai<kai.bi@yoho.cn>
 * @date: 2015/11/17
 */

var $ = require('jquery'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading');

var $action = $('.action'),
    $addressForm = $('.edit-address'),
    $submit = $('.submit'),
    $addAddress = $('.add-address'),
    $footer = $('#yoho-footer'),
    $confim = $('.confim-mask'),
    $pageWrap = $('.page-wrap'),
    $addressItem = $('.address-item'),
    deleteId;

$pageWrap.first().css('min-height', function() {
    return $(window).height() - $('#yoho-header').height() - $footer.height();
});
window.rePosFooter();

$confim.on('touchend', '.cancel', function() {
    deleteId = null;
    $confim.hide();
}).on('touchend', '.confim', function() {
    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/home/delAddress',
        data: {
            id: deleteId
        }
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            res = {};
        }
        if (res.code !== 200) {
            tip.show(res.message || '网络出了点问题~');
        } else {
            window.location.reload();
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
    }).always(function() {
        deleteId = null;
        $confim.hide();
        loading.hideLoadingMask();
    });
});

// 添加地址
if ($addressItem.length < 5) {
    $addAddress.attr('href', $addAddress.data('href'));
}
$addAddress.on('touchend', function() {
    if ($addressItem.length >= 5) {
        tip.show('您最多添加5个收货地址');
        return false;
    }
});

// 删除
$action.on('touchend', '.del', function() {
    deleteId = $(this).data('id');
    $confim.show();
});

$submit.on('touchend', function() {
    $addressForm.submit();
    return false;
}).on('touchstart', function() {
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});