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

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

var $action = $('.action'),
    $addressForm = $('.edit-address'),
    $submit = $('.submit'),
    $addAddress = $('.add-address'),
    $editAddressPage = $('.my-edit-address-page'),
    isSubmiting;

function editAddress(data) {
    data = data || {};
    $addressForm.find('[name="id"]').val(data.id || '');
    $addressForm.find('[name="consignee"]').val(data.consignee || '');
    $addressForm.find('[name="mobile"]').val(data.mobile || '');
    $addressForm.find('[name="area_code"]').val(data.areaCode || '');
    $addressForm.find('[name="area"]').val(data.area || '');
    $addressForm.find('[name="address"]').val(data.address || '');
    $editAddressPage.show();
    $addressForm.find('[name="consignee"]').focus();
}

function deleteAddress(data) {

}

// 添加地址
$addAddress.on('touchend', function() {
    editAddress();
});

// 编辑或删除
$action.on('touchend', '.edit', function() {
    editAddress($(this).data());
}).on('touchend', '.del', function() {
    deleteAddress();
});

$submit.on('touchend', function() {
    $addressForm.submit();
    return false;
});

$addressForm.on('submit', function() {
    if (isSubmiting) {
        return false;
    }
    isSubmiting = true;
    $.ajax({
        method: 'POST',
        url: '/home/saveaddress',
        data: $(this).serialize()
    }).then(function(res) {
        res = res || {};
        console.log(res);
        if (res.code !== 200) {
            tip.show(res.message);
        } else {
            window.location.reload();
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
    }).always(function() {
        isSubmiting = false;
    });
    return false;
});