Authored by ccbikai

地址修改 JS

  1 +/**
  2 + * 地址管理
  3 + * @author: bikai<kai.bi@yoho.cn>
  4 + * @date: 2015/11/17
  5 + */
  6 +
  7 +var $ = require('jquery'),
  8 + tip = require('../plugin/tip');
  9 +
  10 +var $action = $('.action'),
  11 + $addressForm = $('.edit-address'),
  12 + $submit = $('.submit'),
  13 + $addAddress = $('.add-address'),
  14 + $editAddressPage = $('.my-edit-address-page'),
  15 + isSubmiting;
  16 +
  17 +function editAddress(data) {
  18 + data = data || {};
  19 + $addressForm.find('[name="id"]').val(data.id || '');
  20 + $addressForm.find('[name="consignee"]').val(data.consignee || '');
  21 + $addressForm.find('[name="mobile"]').val(data.mobile || '');
  22 + $addressForm.find('[name="area_code"]').val(data.areaCode || '');
  23 + $addressForm.find('[name="area"]').val(data.area || '');
  24 + $addressForm.find('[name="address"]').val(data.address || '');
  25 + $editAddressPage.show();
  26 + $addressForm.find('[name="consignee"]').focus();
  27 +}
  28 +
  29 +function deleteAddress(data) {
  30 +
  31 +}
  32 +
  33 +// 添加地址
  34 +$addAddress.on('touchend', function() {
  35 + editAddress();
  36 +});
  37 +
  38 +// 编辑或删除
  39 +$action.on('touchend', '.edit', function() {
  40 + editAddress($(this).data());
  41 +}).on('touchend', '.del', function() {
  42 + deleteAddress();
  43 +});
  44 +
  45 +$submit.on('touchend', function() {
  46 + $addressForm.submit();
  47 + return false;
  48 +});
  49 +
  50 +$addressForm.on('submit', function() {
  51 + if (isSubmiting) {
  52 + return false;
  53 + }
  54 + isSubmiting = true;
  55 + $.ajax({
  56 + method: 'POST',
  57 + url: '/home/saveaddress',
  58 + data: $(this).serialize()
  59 + }).then(function(res) {
  60 + res = res || {};
  61 + console.log(res);
  62 + if (res.code !== 200) {
  63 + tip.show(res.message);
  64 + } else {
  65 + window.location.reload();
  66 + }
  67 + }).fail(function() {
  68 + tip.show('网络出了点问题~');
  69 + }).always(function() {
  70 + isSubmiting = false;
  71 + });
  72 + return false;
  73 +});