angular2-jsonapi
angular2-jsonapi copied to clipboard
Update attribute without triggering `hasDirtyAttributes` flag
I have the following situation:
I have an ordered list of items and when I change the order on client side, the server expects the information, which element was moved, and on which position it is now. Then the server will internally update the order attribute of all other elements in the list. On client side I do the same (something really simple like items.forEach((item, index) => item.order = index)).
The problem is, that all items, where I changed the order attribute (= all items below the moved item), have now the hasDirtyAttributes flag set to true (because the oldValue and newValue are not equal). But they are not really dirty, as I know that the server updated the order as well.
The real problem occurs, when I move one of the "dirty" items in a way, that its order attribute is the same as at the beginning. Now the hasDirtyAttributes is set to false (because the oldValue and the newValue are equal again) and when I save() the item, it does not send any attributes to the server.
So I needed a way to kind of deep overwrite a models attribute and came up with the following method:
deepPropertyOverwrite(element: typeof JsonApiModel, property: string, value: any) {
if (element[property] === undefined) {
return;
}
element[property] = value;
const symbols = Object.getOwnPropertySymbols(element);
if (!symbols.length) {
return;
}
const attributeMetadata = symbols[0] as any;
const metadata = element[attributeMetadata] && element[attributeMetadata][property];
if (!metadata) {
return;
}
metadata.newValue = value;
metadata.oldValue = value;
metadata.hasDirtyAttributes = false;
}
My question is: should something like this be implemented in the json-api.model.ts or is this already possible in another way I did not think of?