【发布时间】:2020-11-04 10:10:51
【问题描述】:
总的来说,我对 Javascript 和软件开发还是很陌生,因为这是我的第一份工作,现在我正在调试一个与验证相关的严重缺陷。
我有一个场景,我有一个数组,在该数组中,每个产品都有一个标记为 Unit Volume 的属性,其代码为 ASC_Sales_Volume。
当前的缺陷是,当用户配置多个产品,但留下一个单位体积为零的产品时,尽管存在验证检查以防止这种情况发生,但仍允许用户向前导航到下一页.阻止用户前进的条件是 $scope.bpTree.response.errorCheckVolume = true。
使用前开发人员当前编写的内容,当用户单击按钮以触发函数时,forEach 循环会不断将 $scope.bpTree.response.errorCheckVolume 更改回 false。
Next 按钮本身是一个 OOTB 组件,我无法直接修改。
解决此问题的下一个最佳步骤是什么?基本上,当 forEach 评估 Unit Volume 值为 0 的产品时,$scope.bpTree.response.errorCheckVolume 应始终等于 true,而不管后续 Unit Volume 值如何,直到用户更改该特定产品的 Unit Volume 值,然后单击下一步按钮,逻辑再次触发。如果错误记录(如数组中单位体积为 0 的任何产品)大于零,则 errorCheckVolume 应始终评估为真,并且应阻止用户继续进行。
我会使用类似于 Array.prototype.some() 的东西吗?我一直盯着屏幕看了好几个小时,尝试不同的方法都无济于事。
这里是sn-p的代码:
function validateAllPlansUnitVolume(triggeredFromNextBtn) {
var coveragesData = $scope.sortedCoverages[$scope.childProduct.instanceKey || $scope.childProduct.productName || $scope.childProduct.Name],
errorRecords = checkForInvalidPlans(coveragesData, triggeredFromNextBtn);
if(errorRecords > 0) {
$scope.bpTree.response.errorCheckVolume = true;
}
else {
$scope.bpTree.response.errorCheckVolume = false;
}
}
function checkForInvalidPlans(coveragesData, triggeredFromNextBtn){
if(!!coveragesData && coveragesData.length) {
coveragesData.forEach(function(product){
if(!!product.attributeCategories){
unitVolumeAttrNodes = product.attributeCategories.records[0].productAttributes.records.filter((ele) => ele.code == "ASC_Sales_Volume");
if(!!unitVolumeAttrNodes && unitVolumeAttrNodes.length){
unitVolumeAttrNodes.forEach(function(attrNode){
if(unitVolumeAttrNodes[0].userValues === 0){
$scope.bpTree.response.errorCheckVolume = true;
product.unitVolumeErr = true;
product.unitVolumeErrMsg = "Unit Volume cannot be zero. Please update before proceeding to the next screen";
}
else {
product.unitVolumeErrMsg = "";
product.unitVolumeErr = false;
$scope.bpTree.response.errorCheckVolume = false;
}
});
}
}
});
return coveragesData.filter((ele) => ele.unitVolumeErr).length;
}
return 0;
}
【问题讨论】:
标签: javascript angularjs loops foreach angularjs-scope