Jquery,分离两个干扰函数并相互传递变量的最佳方法是什么?
Jquery, what is best way to separate two interfering functions and pass variables to each other?
我知道这可能已经以某种方式得到了回答,同时我将继续尝试寻找解决方案。我只是希望得到一些关于处理这个问题的最佳方法的想法。
我有一个购物车。
我的网站将许多商店链接在一起。
我的网站是基于重量而不是数量。
我的购物车最大容量为 28 克。
如果用户的购物车中有商品,并且他们决定增加重量。旧项目被新项目取代。
在此过程中,旧物品重量被添加到新物品重量中,然后被添加到购物车的当前重量中。我需要从这个过程中排除旧物品的重量,只比较新物品和购物车中的其他物品。
我找到了一个函数,它会调用重复项并获取重量结果。
我从总数中减去金额,一切正常!
错了!我发现获取重复项目权重的函数,如果没有重复项目,将 return false。现在该函数位于另一个基于 returning true 或 false 的函数中。 True 表示该项目是 "added",false 表示它是 "not added"。所以当我的购物车是空的或者没有重复的时候,整个函数过早地结束了。
我之所以发布这个,是因为我注意到有很多方法可以做到这一点,而且很多这些解决方案都已有 5 年历史了。
神奇之处来了
<script>
simpleCart.bind('beforeAdd', function (item) {
var existItem = simpleCart.has(item).get('weight');
var multi_item = simpleCart.quantity();
var cartweight = simpleCart.weight();
var shopid = simpleCart.sid();
var newweight = $( "div.item_weight" ).text();
var itemid = $( "div.item_id" ).text();
var totalweight = parseInt(cartweight) + parseInt(newweight) - parseInt(existItem);
if(shopid != null && shopid != <?=$shopid?> ){
$( "#pop_fail_store" ).popup( "open" )
return false;
}else if (parseInt(totalweight)>=29){
$( "#pop_fail_weight" ).popup( "open" )
return false;
}else if (simpleCart.has(item)){
$( "#pop_update" ).popup( "open" )
return true;
}else {
return true;
}
});
//SUCCESS
simpleCart.bind( "afterAdd" , function( item ){
$( "#pop_success" ).popup( "open" )
});
</script>
当没有项目时,这 return 为 false,并提前终止该功能。
var existItem = simpleCart.has(item).get('weight');
这些是代表HAS EQUALS和GET的函数
has: function (item) {
var match = false;
simpleCart.each(function (testItem) {
if (testItem.equals(item)) {
match = testItem;
}
});
return match;
}
me.equals = function (item) {
for( var label in _data ){
if (Object.prototype.hasOwnProperty.call(_data, label)) {
if (label !== 'quantity' && label !== 'id' && label !== 'price' && label !== 'weight') {
if (item.get(label) !== _data[label]) {
return false;
}
}
}
}
return true;
};
me.get = function (name, skipPrototypes) {
var usePrototypes = !skipPrototypes;
if (isUndefined(name)) {
return name;
}
// return the value in order of the data object and then the prototype
return isFunction(_data[name]) ? _data[name].call(me) :
!isUndefined(_data[name]) ? _data[name] :
isFunction(me[name]) && usePrototypes ? me[name].call(me) :
!isUndefined(me[name]) && usePrototypes ? me[name] :
_data[name];
};
我无法在不弄乱其他函数的情况下更改 get、has 或 equals 函数。我只需要它 returns 的值。
我的第一个计划是将它移出 "before add" 函数并将其保存为一个就绪函数内部的全局变量,然后在我启动 "before add" 函数后调用它?这是最好的方法吗?
或
ajax...
或
创建新功能?
或者我可以添加另一个 if 语句吗?
选择的力量并不总是最好的。我确定我的脸上有一个简单的答案。
也许有人可以告诉我 2017 年最有效的方法。
来自我网站的一个页面 Click Here
提前致谢!
通过查看SimpleCart.has()
和SimpleCart.get()
函数的代码,您只需要在使用前检查has()
的输出。
var hasItem = simpleCart.has(item);
var existItem = hasItem === false ? 0 : hasItem.get('weight');
我知道这可能已经以某种方式得到了回答,同时我将继续尝试寻找解决方案。我只是希望得到一些关于处理这个问题的最佳方法的想法。
我有一个购物车。 我的网站将许多商店链接在一起。 我的网站是基于重量而不是数量。 我的购物车最大容量为 28 克。
如果用户的购物车中有商品,并且他们决定增加重量。旧项目被新项目取代。
在此过程中,旧物品重量被添加到新物品重量中,然后被添加到购物车的当前重量中。我需要从这个过程中排除旧物品的重量,只比较新物品和购物车中的其他物品。
我找到了一个函数,它会调用重复项并获取重量结果。
我从总数中减去金额,一切正常!
错了!我发现获取重复项目权重的函数,如果没有重复项目,将 return false。现在该函数位于另一个基于 returning true 或 false 的函数中。 True 表示该项目是 "added",false 表示它是 "not added"。所以当我的购物车是空的或者没有重复的时候,整个函数过早地结束了。
我之所以发布这个,是因为我注意到有很多方法可以做到这一点,而且很多这些解决方案都已有 5 年历史了。
神奇之处来了
<script>
simpleCart.bind('beforeAdd', function (item) {
var existItem = simpleCart.has(item).get('weight');
var multi_item = simpleCart.quantity();
var cartweight = simpleCart.weight();
var shopid = simpleCart.sid();
var newweight = $( "div.item_weight" ).text();
var itemid = $( "div.item_id" ).text();
var totalweight = parseInt(cartweight) + parseInt(newweight) - parseInt(existItem);
if(shopid != null && shopid != <?=$shopid?> ){
$( "#pop_fail_store" ).popup( "open" )
return false;
}else if (parseInt(totalweight)>=29){
$( "#pop_fail_weight" ).popup( "open" )
return false;
}else if (simpleCart.has(item)){
$( "#pop_update" ).popup( "open" )
return true;
}else {
return true;
}
});
//SUCCESS
simpleCart.bind( "afterAdd" , function( item ){
$( "#pop_success" ).popup( "open" )
});
</script>
当没有项目时,这 return 为 false,并提前终止该功能。
var existItem = simpleCart.has(item).get('weight');
这些是代表HAS EQUALS和GET的函数
has: function (item) {
var match = false;
simpleCart.each(function (testItem) {
if (testItem.equals(item)) {
match = testItem;
}
});
return match;
}
me.equals = function (item) {
for( var label in _data ){
if (Object.prototype.hasOwnProperty.call(_data, label)) {
if (label !== 'quantity' && label !== 'id' && label !== 'price' && label !== 'weight') {
if (item.get(label) !== _data[label]) {
return false;
}
}
}
}
return true;
};
me.get = function (name, skipPrototypes) {
var usePrototypes = !skipPrototypes;
if (isUndefined(name)) {
return name;
}
// return the value in order of the data object and then the prototype
return isFunction(_data[name]) ? _data[name].call(me) :
!isUndefined(_data[name]) ? _data[name] :
isFunction(me[name]) && usePrototypes ? me[name].call(me) :
!isUndefined(me[name]) && usePrototypes ? me[name] :
_data[name];
};
我无法在不弄乱其他函数的情况下更改 get、has 或 equals 函数。我只需要它 returns 的值。
我的第一个计划是将它移出 "before add" 函数并将其保存为一个就绪函数内部的全局变量,然后在我启动 "before add" 函数后调用它?这是最好的方法吗?
或
ajax...
或
创建新功能?
或者我可以添加另一个 if 语句吗?
选择的力量并不总是最好的。我确定我的脸上有一个简单的答案。
也许有人可以告诉我 2017 年最有效的方法。
来自我网站的一个页面 Click Here
提前致谢!
通过查看SimpleCart.has()
和SimpleCart.get()
函数的代码,您只需要在使用前检查has()
的输出。
var hasItem = simpleCart.has(item);
var existItem = hasItem === false ? 0 : hasItem.get('weight');