Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
AT: misusing setValue
"name": "AT: misusing setValue",
"description": "",
"data": {
"myContainer": {
"magicValue": 7
{macro main()}
<button {on click "updateModelBad" /}>Update (bad)</button>
<button {on click "updateModelStillBad" /}>Update (even worse)</button>
<button {on click "updateModelBetter" /}>Update (best)</button>
Magic value is: {@aria:Text {
bind : {
text : {
to: "magicValue"
<p>1. The 'bad' button operation retrieves a reference to 'myContainer' object, modifies the object, and then assigns back this reference to the data model entry. It's basically equivalent to this code:
var a, b;
a = \{\};
b = a;
b.someProperty = 42;
a = b;
It's just juggling the references, <code>a</code> is still the same object after the last assignment. The last operation doesn't really change anything; both <code>a</code> and <code>b</code> are references to the very same object already, so last action is essentally a NOOP.
<p>2. The 'even worse' button tries to circumvent the issue from point 1) by creating a new object, instead of modifying the existing one. This however creates other problems.
Our Text widget is bound to <code>magicValue</code> of <code></code> object at the time of its first evaluation.
the line
this.$json.setValue(, "myContainer", c);
is equivalent in this context to
<pre> = \{...\}
In other words, we're destroying the reference to the original object, to which the widget is bound. Now the data model contains a completely new object, while the widget is still hoping to see updates to the old object, but that reference is lost, so no updates will ever occur any more.
3. The 'best' button implements things correctly - it doesn't change the reference to the original object, but it mutates the <em>properties</em> of that object.<br><br>
<strong>Note that The last button implements the correct behavior, but after clicking the middle button first, it won't work, because it will try to update, though correctly, not the proper object that the widget is observing.</strong> If the last button is not working fine for you, you have to refresh the page.
$prototype : {
updateModelBad: function() {
var c =;
c.magicValue = 42;
c.otherValue = 888;
this.$json.setValue(, "myContainer", c);
updateModelStillBad: function() {
var c = {
magicValue: 42,
otherValue : 888
this.$json.setValue(, "myContainer", c);
updateModelBetter: function() {
this.$json.setValue(, "magicValue", 42);
this.$json.setValue(, "otherValue", 888);

This comment has been minimized.

Copy link
Owner Author

@jakub-g jakub-g commented Oct 17, 2014

Created by Instant Aria Templates, viewable on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment