我是jQuery的新手,所以不要介意这个问题,如果它听起来很愚蠢,但这是我想要做的事情:
我有3个功能,如:
AddToCart Function which adds item to the shopping cart:
//offer_id is the offer which we are trying to add to cart.
addToCart: function(offer_id)
{
},
RemoveFromCart which removes data from the cart
//target is link clicked and event is the click event.
removeFromCart: function(target, event)
{
},
Get the current state of the cart
//return string which represents current state of cart.
getCartItems: function()
{
}
现在我正在尝试做三件事:
content
而且addToCart
的调用次数比some action
要大,所以基本上我们需要检查购物车的当前状态,这是通过调用getCartItems
获得的,如果是Null
而不是addToCart
调用some action
比我们执行content
addToCart
some action
被调用而getCartItems
被调用,所以基本上我们需要检查购物车的当前状态,并通过调用Null
并检查它是否为addToCart
来获取如果我们在购物车中有一些内容,那么如果调用some action
,我们会执行content
。 removeFromCart
而且some action
被称为getCartItems
,所以基本上我们需要检查购物车的当前状态,这是通过调用removeFromCart
获得的,如果它不是空的,那么some action
被调用比我们执行 if there is no content in cart
and addToCart is called than
$(document).track(
);
if there is content in the cart
and addToCart is called than
$(document).track(
);
if there is content in the cart
and removeFromCart is called
$(document).track(
);
我正在尝试做的伪代码:
{{1}}
我最基本的担心是jQuery和JavaScript的新手,所以我不知道如何实现if ... else逻辑以及如何使用jQuery / JavaScript调用函数。
答案 0 :(得分:1)
它与任何其他编程语言类似。
if() {
..
}
else if() {
..
}
else if() {
..
}
但是,由于必须在调用addToCart
和removeFromCart
时执行这些操作,因此一个简单的解决方案是将这些条件放在函数本身中:
addToCart: function(offer_id) {
// add to cart is called and cart IS empty
if(this.getCartItems().length == 0) {
}
// add to cart is called and cart is NOT empty
else {
}
}
removeFromCart: function(target, event) {
// remove was called and cart has items
if(this.getCartItems().length > 0) {
}
}