Authored by htoooth

add once

... ... @@ -200,6 +200,19 @@ function jumpUrl(url) {
window.location.href = url;
}
function once(fn, context) {
var result;
return function() {
if (fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
};
}
window.cookie = cookie;
window.setCookie = setCookie;
... ... @@ -220,5 +233,7 @@ window.registerUrl = registerUrl;
window.jumpUrl = jumpUrl;
window.once = once;
require('./header');
require('./footer');
... ...
... ... @@ -19,7 +19,6 @@ var notify = require('./detail/notify');
var favorite = require('./detail/brand-fav');
var bindEvent = $.Callbacks(); // eslint-disable-line
var clickAcquireCouponEvent = $.Callbacks('unique'); // eslint-disable-line
var Dialog = require('../common/dialog').Dialog;
... ... @@ -108,9 +107,57 @@ bindEvent.add(function() {
var Alert = require('../common/dialog').Alert;
var $moreCoupon = $('.more-coupon');
/** 优惠券开始 **/
var $moreCoupon = $('.more-coupon'),
$couponItem = $('.coupon-item');
var $couponItem = $('.coupon-item');
function getCouponAsync(cid) {
var uid = getUid(); // eslint-disable-line
if (!uid) {
return $.Deferred().reject({data: {refer: window.signinUrl()}}).promise(); // eslint-disable-line
}
return $.getJSON('/product/detail/coupon', {
couponId: cid
}).then(function(result) {
var defer = $.Deferred(); //eslint-disable-line
if (result.code === 200) {
defer.resolve();
} else {
defer.reject(result);
}
return defer.promise();
});
}
function acquireCoupon($ele) {
var couponId = $ele.data('id');
var i = $ele.data('i');
var $item = $($couponItem.get(i));
var status = $item.data('status');
switch (status) {
case 1:
getCouponAsync(couponId).then(function() {
$ele.find('.coupon-status').text(oldStatus.txt);
$ele.find('.coupon-go').empty().append(oldStatus.html);
$item.data('status', 3);
}).fail(function(err) {
if (err.data && err.data.refer) {
return window.jumpUrl(err.data.refer);
}
alert(err.message);
});
break;
default:
window.jumpUrl(brandDomain);
}
}
function openCouponDialog(e) {
var $coupons = $couponItem.map(function(i) {
... ... @@ -147,14 +194,24 @@ bindEvent.add(function() {
}
};
new Dialog({
var dialog = new Dialog({
content: dialogTpl({coupon: couponData}),
className: 'coupon-big'
}).show();
});
dialog.$el.on('click', '.status', function() {
acquireCoupon($(this));
});
dialog.show();
e.stopPropagation();
}
// 打开优惠券列表
$moreCoupon.on('click', openCouponDialog);
/** 优惠券结束 **/
function getSku() {
return $('.size:not(.hide) li.focus').data('sku');
}
... ... @@ -229,6 +286,8 @@ bindEvent.add(function() {
move(e);
});
// 细节图结束
// 促销弹出框
var icon = { // eslint-disable-line
unfold: '',
... ... @@ -246,32 +305,7 @@ bindEvent.add(function() {
}
});
function getCouponAsync(cid) {
var uid = getUid(); // eslint-disable-line
if (!uid) {
return $.Deferred().reject({data: {refer: window.signinUrl()}}).promise(); // eslint-disable-line
}
return $.getJSON('/product/detail/coupon', {
couponId: cid
}).then(function(result) {
var defer = $.Deferred(); //eslint-disable-line
if (result.code === 200) {
defer.resolve();
} else {
defer.reject(result);
}
return defer.promise();
});
}
/**
* 获取当前选择的商品数目
*/
// 获取当前选择的商品数目
function getNum() {
return +$num.text();
}
... ... @@ -476,9 +510,7 @@ bindEvent.add(function() {
resetNum();
}).on('click', '.size-ruler', function() {
// 尺码帮助
$('body,html').animate({
scrollTop: $('.size-info').offset().top
}, 300);
... ... @@ -821,39 +853,6 @@ bindEvent.add(function() {
$sizes.eq($('.colors .focus').index()).find('li').click();
}
}());
// 打开优惠券列表
$moreCoupon.on('click', openCouponDialog);
function acquireCoupon($ele) {
var couponId = $ele.data('id');
var i = $ele.data('i');
var $item = $($couponItem.get(i));
var status = $item.data('status');
switch (status) {
case 1:
getCouponAsync(couponId).then(function() {
$ele.find('.coupon-status').text(oldStatus.txt);
$ele.find('.coupon-go').empty().append(oldStatus.html);
$item.data('status', 3);
}).fail(function(err) {
if (err.data && err.data.refer) {
return window.jumpUrl(err.data.refer);
}
alert(err.message);
});
break;
default:
window.jumpUrl(brandDomain);
}
}
// 清除原来的事件,增加新的事件
clickAcquireCouponEvent.empty();
clickAcquireCouponEvent.add(acquireCoupon);
});
// yas
... ... @@ -861,11 +860,6 @@ require('../common');
require('./detail/latest-walk'); // 最近浏览
// 优惠券的领取
$(document).on('click', '.status', function() {
clickAcquireCouponEvent.fire($(this));
});
// 品牌收藏
$('#brand-favour').click(function() {
var $this = $(this);
... ... @@ -1494,6 +1488,7 @@ $('.package-box').on('click', '#buy-detail', function() {
});
});
// 店铺推荐和最近浏览
$('.bottom-tab').on('click', '.bottom-title', function() {
var $this = $(this),
index = $this.index();
... ...
... ... @@ -6,14 +6,14 @@ exports.getFavNum = function(sid, bid) {
if (result.code === 200) {
return result.data.count;
} else {
return $.Deferred().reject().promise();
return $.Deferred().reject().promise();// eslint-disable-line
}
});
};
exports.addFav = function(sid, bid) {
if (!getUid()) {
return $.Deferred().reject(window.jumpUrl(window.signinUrl())).promise();
if (!getUid()) {// eslint-disable-line
return $.Deferred().reject(window.jumpUrl(window.signinUrl())).promise(); // eslint-disable-line
}
if (sid) {
... ... @@ -48,8 +48,8 @@ exports.addFav = function(sid, bid) {
};
exports.cancelFav = function(sid, bid) {
if (!getUid()) {
return $.Deferred().reject(window.jumpUrl(window.signinUrl())).promise();
if (!getUid()) {// eslint-disable-line
return $.Deferred().reject(window.jumpUrl(window.signinUrl())).promise();// eslint-disable-line
}
if (sid) {
... ...