I'm currently grappling with the challenge of identifying modifications to a complex nested, hierarchical object in an angular environment. Despite my initial hopes of utilizing a $watch
or $watchCollection
expression, I'm facing difficulties in achieving the desired outcome.
Would someone be able to propose a sound approach to tackle this issue?
Here's an example of the data structure that I intend to monitor (in JSON format):
{
"$type":"<>f__AnonymousType7`2[[System.String, mscorlib],[System.Object, mscorlib]], VM.ConfigurationServer",
"Name":"DevelopmentEnvironment",
"Descendents":{
"$type":"<>f__AnonymousType7`2[[System.String, mscorlib],[System.Object, mscorlib]][], VM.ConfigurationServer",
"$values":[{
"$type":"<>f__AnonymousType7`2[[System.String, mscorlib],[System.Object, mscorlib]], VM.ConfigurationServer",
"Name":"MoreSpecificEnvironment",
"Descendents":{
"$type":"<>f__AnonymousType7`2[[System.String, mscorlib],[System.Object, mscorlib]][], VM.ConfigurationServer",
"$values":[{
"$type":"<>f__AnonymousType7`2[[System.String, mscorlib],[System.Object, mscorlib]], VM.ConfigurationServer",
"Name":"level3",
"Descendents":null
}]
}
}]
}
}
As evident, the TypeNameHandling setting of the JSON Serializer is configured as All, which is essential for other components within the system.
My main interest lies in being notified of any alterations to the Name
attribute within any of the nested objects, as well as the possible addition or removal of a Descendent
.
I have experimented with both
scope.$watch(<objectname>, function(){...}, true);
and
scope.watchCollection(<objectname>, function(){...}, true);
These approaches successfully detect changes to the top-level Name
and Descendents
, but fail to register modifications within further nested objects.
Based on my analysis, it appears that the angular.equals
function is unable to identify alterations beyond the top level.
Is there a possibility of incorporating a custom equality comparator?
Is there a method for dynamically altering the watchExpression
? This would involve implementing a watch on each object within the hierarchy, and adjusting the watchExpression
as the hierarchy undergoes changes.