Tweet |
Lighting Component Attributes - Expect the Unexpected
Introduction
A couple of weeks ago my BrightGen colleague, Firoz Mirza, described some unexpected (by us at least) behaviour when dealing with Lightning Component attributes. I was convinced that there must be something else going on so spent some time creating a couple of simple tests, only to realise that he was absolutely correct and attributes work a little differently to how I thought, although if I’m honest, I’ve never really thought about it.
Tales of the Unexpected 1
The basic scenario was assigning a Javascript variable to the result of extracting the attribute via component.get(), then calling another method that also extracted the value of the attribute, but crucially also changed it and set it back into the component. A sample component helper that works like this is:
({ propertyTest : function(cmp) { var testVar=cmp.get('v.test'); alert('Local TestVar = ' + JSON.stringify(testVar)); this.changeAttribute(cmp); alert('Local TestVar after leaving it alone = ' + JSON.stringify(testVar)); }, changeAttribute : function(cmp) { var differentVar=cmp.get('v.test'); differentVar.value1='value1'; cmp.set('v.test', differentVar); } })
The propertyTest function gets the attribute from the component and assigns it to testVar. After showing the value, it then invokes changeAttribute, which gets the attribute and assigns it to a new variable, updates it and then sets it back into the component.
Executing this shows the value of the property is empty, as expected to begin with:
but after the attribute has been updated elsewhere, my local variable reflects this:
Tales of the Unexpected 2
After a bit more digging, I came across something that I expected even less - updating a local variable that stored the result of the component.get() for the attribute, changed the value in the component. So I updated my sample component to test this:
propertyTest2: function(cmp) { var test2=cmp.get('v.test'); test2.value2='value2'; alert('Local test2 = ' + JSON.stringify(test2)); this.checkAttribute(cmp); }, checkAttribute: function(cmp) { var otherVar=cmp.get('v.test'); alert('Other var = ' + JSON.stringify(otherVar)); }
Executing this shows my local property value being updated as expected:
but then retrieving this via another variable showed that it appeared to have been updated in the component:
“Maybe it’s just because everything is happening in the same request” I thought, but after this request completed I executed the first test, so extracting the value in a separate request (transaction) showed that the attribute had indeed been updated in a way that persisted across requests:
Checking the Docs
Convinced that I must have missed this in the docs, I went back to the Lightning Components Developer Guide, Working with Attribute Values in JavaScript section, but only found the following:
component.get(String key) and component.set(String key, Object value) retrieves and assigns values associated with the specified key on the component.
which didn’t add anything to what I already knew.
Asking the Experts
I then asked the question of Salesforce, with my sample components, and received the following response:
The behavior you’re seeing is expected.
component.set() is a signal to the framework that you’ve changed a value. The framework then fires change handlers, rerenders, and so forth.
The underlying value is a JavaScript object, array, primitive, or otherwise. JavaScript object’s and arrays are mutable. Aura’s model requires that you send that signal when you mutate an object or array.
Conclusion
So there you have it - a local variable assigned the result of component.get() gives you a live hook to the component attribute, and if it’s mutable then any changes you make to the local variable update the attribute regardless of whether you call component.set() or not. Good to know!
I think the docs could certainly use with some additional information to make this clear. It’s written down here, at least, so hopefully more people will know going forward!
Related Posts
- Lightning experience utility bar - add an app for that
- Lightning Components - New Tab, New You
- Deploying Lightning Components - Go Big or Go Missing
- Lightning Component Events - Change Types with Care
Another good post Bob! I ran into this issue this morning while splicing elements out of an object array during a helper method. When running the helper method again, I was seeing different results in the UI. Took some time to find the "issue." So for those who are running into this, you can copy the array variable with var.splice() or object with JSON.parse(JSON.stringify(var)) to keep your original attribute value untouched.
ReplyDeleteHi Bob,
ReplyDeleteGreat article!
I'm almost sure that I saw the answer Salesforce gave to you in a Trailhead. Lightning is growing fast, and I do think that the documentation of the AuraFramework lacks clarity.
Let's hope it will get better soon!