To simplify this process, it's best to have the Shape
object enforce the rules itself. After all, that's what a Shape is designed to do, as you pointed out.
The challenge lies in how to empower the Shape to implement these rules. This largely depends on the nature of the Shape and its characteristics.
If the Shape is a POJSO (Plain Old JS Object), there is no built-in mechanism for enforcement.
shapeA = {width:10,height:20,enforce:true}
This means nothing prevents someone from bypassing the enforcement with a line like shapeA.width = 20
and disregarding the intended restrictions.
In a secure es5 environment where getters and setters/Object.defineProperty can be utilized, enforcing constraints such as aspect ratio becomes feasible. Modifying one value like shapeA.width = 20
can trigger an internal method within the object to uphold the desired properties.
For scenarios without ES5 support, explicit setters can be assigned to the object:
shapeA = {getWidth:function(){...}, setWidth: function(){...}, /* etc. */ }
This approach may not be aesthetically pleasing but serves its purpose effectively.
An alternative option could involve handling enforcement within the ShapeService using functions.
Allowing flexibility at input but enforcing rules upon saving is another possibility, though potential errors may linger until the save action is initiated.
Another suggestion could entail utilizing the controller and $scope.$watch
, although monitoring numerous Shapes in this manner might not be ideal, as this logic doesn't truly belong in the controller.
In summary, a cleaner solution would be preferable. Perhaps limiting support to environments compliant with es5 standards could offer a more streamlined approach? :-)