Authored by xuqi

recevice update when focused address update

... ... @@ -171,7 +171,7 @@
\{{#each address}}
<li class="address\{{#isY is_default}} default\{{/isY}}\{{#if focus}} focus\{{/if}}"
data-id="\{{address_id}}" data-name="\{{consignee}}" data-mobile="\{{mobile}}"
data-phone="\{{phone}}" data-areacode="\{{area_code}}" data-address="\{{address}}">
data-phone="\{{phone}}" data-areacode="\{{area_code}}" data-area="\{{area}}"" data-address="\{{address}}">
<div class="address-header"></div>
<div class="address-content">
<p class="default-or-not">
... ...
... ... @@ -74,11 +74,6 @@
<label>设置为默认收货地址</label>
</div>
</div>
\{{#unless dialogAddress}}
<div class="form-group">
<span class="btn operation" id="save-address">保存</span>
</div>
\{{/unless}}
</div>
</div>
</form>
... ...
... ... @@ -9,6 +9,8 @@ var $ = require('yoho-jquery'),
cascadingAddress = require('../plugins/cascading-address'),
popup = require('../plugins/dialog');
var $receiver = $('#receiver');
var Dialog = popup.Dialog,
Confirm = popup.Confirm;
... ... @@ -101,10 +103,14 @@ function validateAddress($el) {
return pass;
}
// 更新收货信息:姓名,手机号码,区域,详细
function receiver(ad) {
$receiver.html(ad.consignee + ' ' + ad.mobile + ' ' + ad.area + ' ' + ad.address);
}
// 地址弹窗Factory
function addressDialogFactory(opt, $the) {
var tplData = $.extend({
dialogAddress: true,
checked: true
}, opt);
... ... @@ -154,20 +160,30 @@ function addressDialogFactory(opt, $the) {
'area_code': areaCode // eslint-disable-line
}
}).then(function(data) {
var updated;
if (data.code === 200) {
updated = {
consignee: consignee,
address: detail,
mobile: mobile,
phone: phone,
area_code: data.data.area_code, // eslint-disable-line
id: opt.id,
focus: $the.hasClass('focus'),
default: $the.hasClass('default'),
area: $el.address.getAreaLabels().replace(/,/g, ' ')
};
$the.before(addressTpl({
address: [{
consignee: consignee,
address: detail,
mobile: mobile,
phone: phone,
area_code: data.data.area_code, // eslint-disable-line
id: opt.id,
focus: $the.hasClass('focus'),
area: $el.address.getAreaLabels().replace(/,/g, ' ')
}]
address: [updated]
}));
// 如果当前地址正在被使用,则更新收货信息
if ($the.hasClass('focus')) {
receiver(updated);
}
$the.remove();
address.close();
}
... ... @@ -199,6 +215,10 @@ function addressDialogFactory(opt, $the) {
$('#address-list').prepend(addressTpl({
address: [the]
}));
// 新地址默认使用,更新收货信息
receiver(the);
address.close();
}
});
... ... @@ -235,11 +255,7 @@ function initAddressContent($el, areaCode) {
});
if (areaCode) {
$el.address.setAddress({
province: '32',
city: '3201',
dist: areaCode
});
$el.address.setAddress(areaCode);
}
// init default
... ... @@ -252,9 +268,17 @@ function initAddressContent($el, areaCode) {
});
}
function newAddress() {
/**
* 新增地址
* @param isInit Boolean 是否地址列表无地址(首次添加不显示取消按钮)
*/
function newAddress(isInit) {
var address = addressDialogFactory();
if (isInit) {
address.$el.addClass('is-init');
}
initAddressContent(address.$el);
address.show();
... ... @@ -279,6 +303,14 @@ $('.address-list').on('click', '.address', function() {
$this.addClass('focus');
$this.siblings('.focus').removeClass('focus');
// 切换地址后切换收货信息
receiver({
consignee: $this.data('name'),
area: $this.data('area'),
mobile: $this.data('mobile'),
address: $this.data('address')
});
}).on('click', '.modify', function(e) {
... ... @@ -357,10 +389,6 @@ $('.address-list').on('click', '.address', function() {
e.stopPropagation();
});
function receiver(ad) {
$('#receiver').html(ad.consignee + ' ' + ad.mobile + ' ' + ad.area + ' ' + ad.address);
}
// 页面加载时请求地址列表,若有则展示列表;若无则直接显示新建弹窗并不可被关闭
$.ajax({
url: '/me/address/list'
... ... @@ -372,7 +400,7 @@ $.ajax({
if (list.length === 0) {
// new address
newAddress();
newAddress(true);
} else {
$('#address-list').append(addressTpl({
address: data.data
... ...