Skip to content

Commit d946864

Browse files
committed
Add a way of implementing pessimistic locking
1 parent ca8eba5 commit d946864

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/client/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ declare module fng {
474474
formSchema: IControlledFormSchema;
475475
baseSchema: () => Array<any>;
476476
setFormDirty: any;
477+
dirtyChecked?: boolean;
477478
add: any;
478479
hideTab: (event, tabTitle: string, hiddenTabArrayProp: string) => void;
479480
addTab: (event, tabTitle: string, hiddenTabArrayProp: string) => void;

src/client/js/services/record-handler.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,14 @@ module fng.services {
744744
processLookupHandlers($scope.record, {});
745745
}
746746

747+
function processDirtyFlag(resp:{dirty?: boolean, dirtyMessage?: string}) {
748+
if (resp?.dirty === true) {
749+
$scope.cancel();
750+
setTimeout(() => {
751+
$scope.showError(resp.dirtyMessage, "Record Locked");
752+
})
753+
}
754+
}
747755
if (listOnly) {
748756
ctrlState.allowLocationChange = true;
749757
} else {
@@ -761,7 +769,18 @@ module fng.services {
761769
$scope.$watch(
762770
"record",
763771
function (newValue, oldValue) {
764-
if (newValue !== oldValue) {
772+
if ($scope.phase === "ready" && !$scope.dirtyChecked && Object.keys(oldValue).length > 0 && $scope.topLevelFormName && $scope[$scope.topLevelFormName].$dirty && typeof $scope.dataEventFunctions?.checkDirty === "function") {
773+
$scope.dirtyChecked = true;
774+
// An opportunity to ask whether we can edit this document or not - pessimistic locking can be implemented by using this
775+
const checkDirty = $scope.dataEventFunctions.checkDirty(newValue, oldValue);
776+
if (typeof checkDirty?.then === "function") {
777+
checkDirty.then((resp) => {
778+
processDirtyFlag(resp);
779+
})
780+
} else {
781+
processDirtyFlag(checkDirty);
782+
}
783+
}
765784
if (
766785
Object.keys(oldValue).length > 0 &&
767786
$scope.dropConversionWatcher
@@ -814,7 +833,6 @@ module fng.services {
814833
title = title.trimEnd() + (formsAngular.title.suffix || "");
815834
$window.document.title = title.replace(/<\/?[^>]+(>|$)/g, "");
816835
}
817-
}
818836
},
819837
true
820838
);
@@ -1441,7 +1459,7 @@ module fng.services {
14411459
// we already have an error showing, so clear timeout and don't overwrite it
14421460
$scope.clearTimeout();
14431461
}
1444-
if ($scope.errorMessage) {
1462+
if ($scope.errorMessage && $scope.errorMessage.indexOf(error) === -1) {
14451463
$scope.errorMessage += "<br /><br />";
14461464
} else {
14471465
$scope.errorMessage = "";

0 commit comments

Comments
 (0)