3

I was playing around with lightning components, and I came across a weird situation.

Data binding for attribute type object doesn't work.

here is minimum viable code to reproduce this behavior.

<aura:application>

    <aura:attribute type="Object" name="myObject" default="{'Name':'Dummy'}"></aura:attribute>

    <lightning:input type="text" label="Name" name="myName" value="{!v.myObject.Name}" />


    Debug: 
    {!v.myObject.Name}
</aura:application>

Nothing fancy, I declared an attribute and using it in Lightning: input to check binding.(I tried with UI:InputText same issue).

According to this documentation there are issues with serialization when using attribute of type Object when doing server downtrip. As I am not doing any, it should be fine.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_attr_types_object.htm

I can change the atrribute type to Map to make it work, I want to know if its the expected behaviour for attribute of type Object or am missing something silly.

Edit: So I tried removing the default and doing initialization in init event. Same behaviour.

  • Great question. Nice example of MVC. I've hit the same problem so your question (still unanswered) is saving me time. Thanks. I hope someone can say for sure if Object is supported. Another question came back with the same answer (to use Map). salesforce.stackexchange.com/questions/151673/… – Bryan Jun 9 '18 at 15:12
4

The only situation I've noticed the type attribute having an impact is in the parsing of the default value. Changing to Map or SObject will get your default parsed.

This value can't be relevant to basic JavaScript syntax by the very nature of JavaScript's weak typing. But when the .cmp is processed specific logic can be run. There may also be some type-specific logic inside the client-side Aura framework but I haven't noticed it.

  • Yeah.. changing to map works, I wanted to know if that's expected behaviour for attribute of type Object – Pranay Jaiswal Jun 7 '18 at 10:21
  • @PranayJaiswal Makes sense to me that extra type information is needed to decide whether to just take the default value as a string or to parse it assuming JSON into an object. – Keith C Jun 7 '18 at 10:23
  • I removed the default attribute and wrote initialization on init event. Same, I cant make Object work. – Pranay Jaiswal Jun 7 '18 at 10:30
  • @PranayJaiswal I think I've done that and remember it working. Must have been something different in what I was doing. – Keith C Jun 7 '18 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.