Last active
December 11, 2018 16:33
-
-
Save itsmebasti/6b85cf8dd8383e88a3404b852a20d306 to your computer and use it in GitHub Desktop.
Lightning Value Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:application > | |
<!-- Value Providers --> | |
<c:XYZ context="{!this}" /> | |
<c:valueProvider context="{!this}" name="X" values="{Y: 'y', Z:'z'}" /> | |
<!-- Events --> | |
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> | |
<!-- Markup --> | |
<!-- Note: this is how simple you can use the values in the markup --> | |
{!XYZ.FOO} | |
{!XYZ.BAR} | |
<br /> | |
{!X.Y} | |
{!X.Z} | |
</aura:application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
doInit: function(cmp, evt, hlp) { | |
// Note: this is how simple you use it in your context component (or app) | |
console.log(XYZ.FOO); | |
console.log(X.Y); | |
// Note: this is how you could also see the markup value provider. | |
console.log(cmp.get("XYZ.BAR")); | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component extensible="true" > | |
<aura:attribute name="name" type="String" required="true" /> | |
<aura:attribute name="values" type="Map" required="true" /> | |
<aura:attribute name="context" type="Map" required="true"/> | |
<!-- Events --> | |
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> | |
</aura:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
doInit: function(cmp, evt, hlp) { | |
const providerName = cmp.get("v.name"); | |
const values = cmp.get("v.values"); | |
// Note: add the markup porvider to the context component | |
cmp.get("v.context").addValueProvider(providerName, { | |
get: (key) => values[key] | |
}); | |
// Note: add the javascript provider to the window | |
Object.defineProperty(window, providerName, { | |
writable: true, | |
value: values | |
}); | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component extends="c:valueProvider"> | |
<aura:set attribute="name" value="XYZ" /> | |
<aura:set attribute="values" value="{FOO:'Foo', BAR: 'Bar'}" /> | |
</aura:component> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment