9

Trying to create a custom Navigation Bar and display menu entries based on user privileges.

I defined this attribute in my component:

<aura:attribute name="navItemPrivs" type="Map" default="{ Task: false }"/>

In the JS controller and helper I'm able to access and modify it using navItemPrivs['Task'], but when I try and use it inside an aura:if expression:

<aura:if isTrue="{!and(v.navItemLabel1 != undefined, v.navItemPrivs['Task'] == true}">

I get an error:

expecting a positive integer, found ''Task'' at column 50 of expression: and(v.navItemLabel1 != undefined, v.navItemPrivs['Task'] == true

I can't access it using v.navItemPrivs[0] because the "0" key isn't defined (nor do I want it to be).

Is there something I'm doing wrong and this isn't the way to do it? Because I don't see why I would not be able to access Map values if they exist in the framework..

  • I suggest create a new child component and pass the map in that component. in that component js controller use javascript method to get the value from map and check if it task = true then hide/show – Ratan Paul Jun 27 '16 at 7:35
  • 1
    Hm, good idea, I'll try that + extend my current component to still be able to use the code there. Hopefully the LockerService won't break anything. Thanks. – Poster Jun 27 '16 at 7:40
  • Didn't work.. got the same error when doing this: <c:navBarItem testAttr="{!v.navItemPrivs['Task']}" /> – Poster Jun 27 '16 at 12:29
  • No..... you need to send this map to controller and check it contains task or not.... check this answer salesforce.stackexchange.com/a/111175/18731 how used inner component – Ratan Paul Jun 27 '16 at 13:00
  • 1
    You can access the attribute as v.navItemPrivs.Task and that should work just fine. However what you can't do is bind to dynamically named keys (e.g. you can't do v.myMap[v.myKey] in Lightning so far). Be aware that under the current way Locker functions (as of Summer '16 Patch 10.0) if you want a Map/Object key binding to be successful the key must exist when you first push the object to the v.attribute it will reside in. – Charles T Jun 27 '16 at 14:01
10

You can access the attribute as v.navItemPrivs.Task and that should work just fine. Object properties are accessible by dot notation but not brackets unless they are integers.

Unfortunately you can't bind to dynamically named keys (e.g. you can't do v.myMap[v.myKey] in Lightning so far). Be aware that under the current way Locker functions (as of Summer '16 Patch 10.0) if you want a Map/Object key binding to be successful the key must exist when you first push the object to the v.attribute it will reside in.

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.