What is the cmp.addValueProvider() feature designed for?












7














To implement something like a public ValueProvider I implemented a component combining the features out there to have static values available in js and markup. Now I'm having trouble using the addValueProvider Feature (used for the markup side) since there is no documentation out there.



To make static values accessible for any component, I need to reference the value providing component in the concrete component like:



<!-- Value Providers -->
<c:THINGS context="{!this}" />


THINGS.cmp looks like:



<aura:component >
<aura:attribute name="context" type="Map" required="true"/>

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>


THINGSController.js



({
doInit: function(cmp, evt, hlp) {
const things = { FOO: "Foo", BAR: "Bar"};

cmp.get("v.context").addValueProvider("THINGS", {get: ()=>things});
}
})




Now I should be able to reference FOO like this:



<!-- Value Providers -->
<c:THINGS context="{!this}" />

<!-- Markup -->
<p>{!THINGS.FOO}</p>


The problem I encountered here is, that THINGS.FOO returns the whole Map, what renders to [object Object].



When I discovered this, I started playing around and figured out, that THINGS behaves really weird.



An init handler on my concrete component looks like this:



<aura:handler name="init" value="{!this}" action="{!c.doInit}" />


ctrl (JSON.parse&stringify to get around the proxy issue):



doInit: function(cmp, evt, hlp) {
JSON.parse(JSON.stringify( cmp.get("THINGS.value") ));
JSON.parse(JSON.stringify( cmp.get("THINGS.FOO") ));
JSON.parse(JSON.stringify( cmp.get("THINGS.x") ));
JSON.parse(JSON.stringify( cmp.get("THINGS.value.FOO") ));
JSON.parse(JSON.stringify( cmp.get("THINGS.asdasd.asdasd.asdd.ads") ));
}


The weird thing here is, that they all returned {FOO: "Foo", BAR: "Bar"}



cmp.get("THINGS") leads to an error message, not knowing what THINGS means.



Conclusion:



Value providers do only work on single values, (or maybe even lists, when we decide to iterate the result.)



cmp.addValueProvider("FOO", {get: ()=>"Foo"});


I hope you agree that this isn't a value provider, it's just a complex way to store a single value.



Quesion:



How are we supposed to use the setValueProvider feature? all I found was this answer that doesn't seem to be fully accurate.



Is there a chance to store simple key value pairs in these providers? if not, why? And where except the Aura Documentation is it documented? All it states is




Adds Custom ValueProviders to a component



Parameters key : String string by which to identify the
valueProvider. Used in expressions in markup, etc.



valueProvider : Object the object to request data from. Must
implement .get(expression), can implement .set(key,value).




Update:



Thanks to Jonathon I was able to solve the mystery and created this gist to share my generic value Provider:



Lightning Value Provider



I decided to make provider names capitalized, so where ever I use it, it does not look like a component, but represents static finals. In our product I use it to port Enums or similar constructs from apex into lightning.










share|improve this question





























    7














    To implement something like a public ValueProvider I implemented a component combining the features out there to have static values available in js and markup. Now I'm having trouble using the addValueProvider Feature (used for the markup side) since there is no documentation out there.



    To make static values accessible for any component, I need to reference the value providing component in the concrete component like:



    <!-- Value Providers -->
    <c:THINGS context="{!this}" />


    THINGS.cmp looks like:



    <aura:component >
    <aura:attribute name="context" type="Map" required="true"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    </aura:component>


    THINGSController.js



    ({
    doInit: function(cmp, evt, hlp) {
    const things = { FOO: "Foo", BAR: "Bar"};

    cmp.get("v.context").addValueProvider("THINGS", {get: ()=>things});
    }
    })




    Now I should be able to reference FOO like this:



    <!-- Value Providers -->
    <c:THINGS context="{!this}" />

    <!-- Markup -->
    <p>{!THINGS.FOO}</p>


    The problem I encountered here is, that THINGS.FOO returns the whole Map, what renders to [object Object].



    When I discovered this, I started playing around and figured out, that THINGS behaves really weird.



    An init handler on my concrete component looks like this:



    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />


    ctrl (JSON.parse&stringify to get around the proxy issue):



    doInit: function(cmp, evt, hlp) {
    JSON.parse(JSON.stringify( cmp.get("THINGS.value") ));
    JSON.parse(JSON.stringify( cmp.get("THINGS.FOO") ));
    JSON.parse(JSON.stringify( cmp.get("THINGS.x") ));
    JSON.parse(JSON.stringify( cmp.get("THINGS.value.FOO") ));
    JSON.parse(JSON.stringify( cmp.get("THINGS.asdasd.asdasd.asdd.ads") ));
    }


    The weird thing here is, that they all returned {FOO: "Foo", BAR: "Bar"}



    cmp.get("THINGS") leads to an error message, not knowing what THINGS means.



    Conclusion:



    Value providers do only work on single values, (or maybe even lists, when we decide to iterate the result.)



    cmp.addValueProvider("FOO", {get: ()=>"Foo"});


    I hope you agree that this isn't a value provider, it's just a complex way to store a single value.



    Quesion:



    How are we supposed to use the setValueProvider feature? all I found was this answer that doesn't seem to be fully accurate.



    Is there a chance to store simple key value pairs in these providers? if not, why? And where except the Aura Documentation is it documented? All it states is




    Adds Custom ValueProviders to a component



    Parameters key : String string by which to identify the
    valueProvider. Used in expressions in markup, etc.



    valueProvider : Object the object to request data from. Must
    implement .get(expression), can implement .set(key,value).




    Update:



    Thanks to Jonathon I was able to solve the mystery and created this gist to share my generic value Provider:



    Lightning Value Provider



    I decided to make provider names capitalized, so where ever I use it, it does not look like a component, but represents static finals. In our product I use it to port Enums or similar constructs from apex into lightning.










    share|improve this question



























      7












      7








      7


      2





      To implement something like a public ValueProvider I implemented a component combining the features out there to have static values available in js and markup. Now I'm having trouble using the addValueProvider Feature (used for the markup side) since there is no documentation out there.



      To make static values accessible for any component, I need to reference the value providing component in the concrete component like:



      <!-- Value Providers -->
      <c:THINGS context="{!this}" />


      THINGS.cmp looks like:



      <aura:component >
      <aura:attribute name="context" type="Map" required="true"/>

      <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
      </aura:component>


      THINGSController.js



      ({
      doInit: function(cmp, evt, hlp) {
      const things = { FOO: "Foo", BAR: "Bar"};

      cmp.get("v.context").addValueProvider("THINGS", {get: ()=>things});
      }
      })




      Now I should be able to reference FOO like this:



      <!-- Value Providers -->
      <c:THINGS context="{!this}" />

      <!-- Markup -->
      <p>{!THINGS.FOO}</p>


      The problem I encountered here is, that THINGS.FOO returns the whole Map, what renders to [object Object].



      When I discovered this, I started playing around and figured out, that THINGS behaves really weird.



      An init handler on my concrete component looks like this:



      <aura:handler name="init" value="{!this}" action="{!c.doInit}" />


      ctrl (JSON.parse&stringify to get around the proxy issue):



      doInit: function(cmp, evt, hlp) {
      JSON.parse(JSON.stringify( cmp.get("THINGS.value") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.FOO") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.x") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.value.FOO") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.asdasd.asdasd.asdd.ads") ));
      }


      The weird thing here is, that they all returned {FOO: "Foo", BAR: "Bar"}



      cmp.get("THINGS") leads to an error message, not knowing what THINGS means.



      Conclusion:



      Value providers do only work on single values, (or maybe even lists, when we decide to iterate the result.)



      cmp.addValueProvider("FOO", {get: ()=>"Foo"});


      I hope you agree that this isn't a value provider, it's just a complex way to store a single value.



      Quesion:



      How are we supposed to use the setValueProvider feature? all I found was this answer that doesn't seem to be fully accurate.



      Is there a chance to store simple key value pairs in these providers? if not, why? And where except the Aura Documentation is it documented? All it states is




      Adds Custom ValueProviders to a component



      Parameters key : String string by which to identify the
      valueProvider. Used in expressions in markup, etc.



      valueProvider : Object the object to request data from. Must
      implement .get(expression), can implement .set(key,value).




      Update:



      Thanks to Jonathon I was able to solve the mystery and created this gist to share my generic value Provider:



      Lightning Value Provider



      I decided to make provider names capitalized, so where ever I use it, it does not look like a component, but represents static finals. In our product I use it to port Enums or similar constructs from apex into lightning.










      share|improve this question















      To implement something like a public ValueProvider I implemented a component combining the features out there to have static values available in js and markup. Now I'm having trouble using the addValueProvider Feature (used for the markup side) since there is no documentation out there.



      To make static values accessible for any component, I need to reference the value providing component in the concrete component like:



      <!-- Value Providers -->
      <c:THINGS context="{!this}" />


      THINGS.cmp looks like:



      <aura:component >
      <aura:attribute name="context" type="Map" required="true"/>

      <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
      </aura:component>


      THINGSController.js



      ({
      doInit: function(cmp, evt, hlp) {
      const things = { FOO: "Foo", BAR: "Bar"};

      cmp.get("v.context").addValueProvider("THINGS", {get: ()=>things});
      }
      })




      Now I should be able to reference FOO like this:



      <!-- Value Providers -->
      <c:THINGS context="{!this}" />

      <!-- Markup -->
      <p>{!THINGS.FOO}</p>


      The problem I encountered here is, that THINGS.FOO returns the whole Map, what renders to [object Object].



      When I discovered this, I started playing around and figured out, that THINGS behaves really weird.



      An init handler on my concrete component looks like this:



      <aura:handler name="init" value="{!this}" action="{!c.doInit}" />


      ctrl (JSON.parse&stringify to get around the proxy issue):



      doInit: function(cmp, evt, hlp) {
      JSON.parse(JSON.stringify( cmp.get("THINGS.value") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.FOO") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.x") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.value.FOO") ));
      JSON.parse(JSON.stringify( cmp.get("THINGS.asdasd.asdasd.asdd.ads") ));
      }


      The weird thing here is, that they all returned {FOO: "Foo", BAR: "Bar"}



      cmp.get("THINGS") leads to an error message, not knowing what THINGS means.



      Conclusion:



      Value providers do only work on single values, (or maybe even lists, when we decide to iterate the result.)



      cmp.addValueProvider("FOO", {get: ()=>"Foo"});


      I hope you agree that this isn't a value provider, it's just a complex way to store a single value.



      Quesion:



      How are we supposed to use the setValueProvider feature? all I found was this answer that doesn't seem to be fully accurate.



      Is there a chance to store simple key value pairs in these providers? if not, why? And where except the Aura Documentation is it documented? All it states is




      Adds Custom ValueProviders to a component



      Parameters key : String string by which to identify the
      valueProvider. Used in expressions in markup, etc.



      valueProvider : Object the object to request data from. Must
      implement .get(expression), can implement .set(key,value).




      Update:



      Thanks to Jonathon I was able to solve the mystery and created this gist to share my generic value Provider:



      Lightning Value Provider



      I decided to make provider names capitalized, so where ever I use it, it does not look like a component, but represents static finals. In our product I use it to port Enums or similar constructs from apex into lightning.







      lightning-aura-components documentation value-providers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 11 '18 at 16:27

























      asked Dec 11 '18 at 12:51









      Basti

      3,9231949




      3,9231949






















          1 Answer
          1






          active

          oldest

          votes


















          7














          The value provider's get method requires a key to retrieve the value from the map/object. So with your value providers get method returning the entire map you will always get the output of [object Object]. By changing your get method in the addValueProvider function to .addValueProvider("THINGS", {get: (k)=>things[k]});,
          when retrieving {!THINGS.FOO} you will get Foo and likewise with {!THINGS.BAR} you would get Bar





          Additionally if you also want to be able to update or add new values to the value provider you need to also include a set method.
          .addValueProvider("THINGS", {get: (k)=>things[k], set: (k, v)=>things[k]=v});






          share|improve this answer





















          • Oh, wow, totally missed that get(expression) thing in the documentation. Thank you so much, I already gave up on having a proper value provider, you saved my feature :D
            – Basti
            Dec 11 '18 at 14:59










          • No problem. Unfortunately this happens to us all
            – Jonathon Chambers
            Dec 11 '18 at 15:12










          • I prepared a gist in case you are interested in the results :) thanks again! Lightning Value Provider
            – Basti
            Dec 11 '18 at 16:29











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242131%2fwhat-is-the-cmp-addvalueprovider-feature-designed-for%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          The value provider's get method requires a key to retrieve the value from the map/object. So with your value providers get method returning the entire map you will always get the output of [object Object]. By changing your get method in the addValueProvider function to .addValueProvider("THINGS", {get: (k)=>things[k]});,
          when retrieving {!THINGS.FOO} you will get Foo and likewise with {!THINGS.BAR} you would get Bar





          Additionally if you also want to be able to update or add new values to the value provider you need to also include a set method.
          .addValueProvider("THINGS", {get: (k)=>things[k], set: (k, v)=>things[k]=v});






          share|improve this answer





















          • Oh, wow, totally missed that get(expression) thing in the documentation. Thank you so much, I already gave up on having a proper value provider, you saved my feature :D
            – Basti
            Dec 11 '18 at 14:59










          • No problem. Unfortunately this happens to us all
            – Jonathon Chambers
            Dec 11 '18 at 15:12










          • I prepared a gist in case you are interested in the results :) thanks again! Lightning Value Provider
            – Basti
            Dec 11 '18 at 16:29
















          7














          The value provider's get method requires a key to retrieve the value from the map/object. So with your value providers get method returning the entire map you will always get the output of [object Object]. By changing your get method in the addValueProvider function to .addValueProvider("THINGS", {get: (k)=>things[k]});,
          when retrieving {!THINGS.FOO} you will get Foo and likewise with {!THINGS.BAR} you would get Bar





          Additionally if you also want to be able to update or add new values to the value provider you need to also include a set method.
          .addValueProvider("THINGS", {get: (k)=>things[k], set: (k, v)=>things[k]=v});






          share|improve this answer





















          • Oh, wow, totally missed that get(expression) thing in the documentation. Thank you so much, I already gave up on having a proper value provider, you saved my feature :D
            – Basti
            Dec 11 '18 at 14:59










          • No problem. Unfortunately this happens to us all
            – Jonathon Chambers
            Dec 11 '18 at 15:12










          • I prepared a gist in case you are interested in the results :) thanks again! Lightning Value Provider
            – Basti
            Dec 11 '18 at 16:29














          7












          7








          7






          The value provider's get method requires a key to retrieve the value from the map/object. So with your value providers get method returning the entire map you will always get the output of [object Object]. By changing your get method in the addValueProvider function to .addValueProvider("THINGS", {get: (k)=>things[k]});,
          when retrieving {!THINGS.FOO} you will get Foo and likewise with {!THINGS.BAR} you would get Bar





          Additionally if you also want to be able to update or add new values to the value provider you need to also include a set method.
          .addValueProvider("THINGS", {get: (k)=>things[k], set: (k, v)=>things[k]=v});






          share|improve this answer












          The value provider's get method requires a key to retrieve the value from the map/object. So with your value providers get method returning the entire map you will always get the output of [object Object]. By changing your get method in the addValueProvider function to .addValueProvider("THINGS", {get: (k)=>things[k]});,
          when retrieving {!THINGS.FOO} you will get Foo and likewise with {!THINGS.BAR} you would get Bar





          Additionally if you also want to be able to update or add new values to the value provider you need to also include a set method.
          .addValueProvider("THINGS", {get: (k)=>things[k], set: (k, v)=>things[k]=v});







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 11 '18 at 14:06









          Jonathon Chambers

          1889




          1889












          • Oh, wow, totally missed that get(expression) thing in the documentation. Thank you so much, I already gave up on having a proper value provider, you saved my feature :D
            – Basti
            Dec 11 '18 at 14:59










          • No problem. Unfortunately this happens to us all
            – Jonathon Chambers
            Dec 11 '18 at 15:12










          • I prepared a gist in case you are interested in the results :) thanks again! Lightning Value Provider
            – Basti
            Dec 11 '18 at 16:29


















          • Oh, wow, totally missed that get(expression) thing in the documentation. Thank you so much, I already gave up on having a proper value provider, you saved my feature :D
            – Basti
            Dec 11 '18 at 14:59










          • No problem. Unfortunately this happens to us all
            – Jonathon Chambers
            Dec 11 '18 at 15:12










          • I prepared a gist in case you are interested in the results :) thanks again! Lightning Value Provider
            – Basti
            Dec 11 '18 at 16:29
















          Oh, wow, totally missed that get(expression) thing in the documentation. Thank you so much, I already gave up on having a proper value provider, you saved my feature :D
          – Basti
          Dec 11 '18 at 14:59




          Oh, wow, totally missed that get(expression) thing in the documentation. Thank you so much, I already gave up on having a proper value provider, you saved my feature :D
          – Basti
          Dec 11 '18 at 14:59












          No problem. Unfortunately this happens to us all
          – Jonathon Chambers
          Dec 11 '18 at 15:12




          No problem. Unfortunately this happens to us all
          – Jonathon Chambers
          Dec 11 '18 at 15:12












          I prepared a gist in case you are interested in the results :) thanks again! Lightning Value Provider
          – Basti
          Dec 11 '18 at 16:29




          I prepared a gist in case you are interested in the results :) thanks again! Lightning Value Provider
          – Basti
          Dec 11 '18 at 16:29


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242131%2fwhat-is-the-cmp-addvalueprovider-feature-designed-for%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Plaza Victoria

          Puebla de Zaragoza

          Musa