Lightning component attributes are used to pass data into a component. The attribute can a basic type (string, number, boolean), object, collection, etc. I have a React background and am used to passing callbacks as component attributes. Is there a way to pass a function type into a Lightning component?
I'm thinking something like this:
<aura:attribute name="getSuggestions" required="true" type="Function" description="Callback to get the suggestions to display to the user" />
EDIT:
Here's an example of <ui:button />
. The callback c.save
is passed to the press attribute.
<ui:button label="Save" class="slds-button" press="{!c.save}"/>
Say I want to create my own implementation of <ui:button />
called myButton
. I would never do this in practice, but it provides a good example of passing a callback to a child component.
In my custom button, I have declared three attributes: label, class, and press. I want to know how to implement the press attribute. The example below doesn't work.
myButton.cmp:
<aura:component>
<aura:attribute name="label" required="true" type="String" />
<aura:attribute name="class" required="false" type="String" />
<aura:attribute name="press" required="true" type="Object" />
<input type="button" value="{!v.label}" class="{!v.class}" onclick="{!c.onClick}" />
</aura:component>
myButtonController.js:
({
onClick : function(component, event, helper) {
// Do some cross-cutting action like track button clicks.
helper.trackButtonClick(component, event);
var press = component.get('v.press');
press(); // This won't work because it's not a function. But it should be! I passed it a function!
}
})
myButtonConsumer.cmp
<aura:component>
<c:myButton label="OK" class="slds-button" press="{!c.onOk}" />
</aura:component>
myButtonConsumerController.js
({
onOk: function() {
alert('OK pressed!');
}
})
I pass the function c.onOk
to the press attribute, but when I invoke component.get('v.press')
, it doesn't return a function. It seems like the Lightning framework wraps it somehow. How do I get at the inner value and execute it?
Or perhaps there's a better way to implement this?