Maintaining relationship of the variables in JavaScript using ConstraintJS.
Let’s suppose there are two elements elem1 and elem2. The element elem2 depends on the value of elem1 for example: elem2 = elem1+5 (where elem1 = 5).
So elem2 has the value of 10 and now suppose elem1 has been changed to 15 then in usual JavaScript the value of the elem2 will not be changed.
This is general behavior of the relationship of the value based variables. You can use the ConstraintJs to solve this problem in JavaScript.
function ChkConstraint() { var elem1,elem2; elem1 = 5; elem2 = elem1 + 5; // Here elem2 = 10 elem1 = 15; alert(elem2); // You can see here that elem2 not changed }
You can maintain the value of the elem2 easily in the JavaScript based on the value of the elem1
function ChkConstraint() { var elem1,elem2; elem1 = cjs(5); elem2 = elem1.add(5); // Here elem2 = 10 elem1.set(15); alert(elem2); // You can see here that elem2 has been changed // Now elemt2 = 20 }
So whenever elem1 changes, value of elem2 automatically updates with it. You can walkthrough for several built-in utility methods to create new dependent variables
ConstraintJS is a new open-source JavaScript library that make easy to develop the interactive web applications. It has small size and you can use it with any of the other JavaScript libraries including the jQuery.