The <aura:method>
declaration belongs on the component receiving the call. The method is considered part of the public API for that component.
In your scenario, the method would be declared on the parent, which is inverted from the typical scenario where child-to-parent communication uses events and parent-to-child communication uses public methods. Your child component will need to receive a reference to its parent and store it in an attribute in order to make the method call. (See example below).
The presence of <aura:attribute>
declarations as part of the <aura:method>
is a little confusing. The method's attributes actually define the parameters of the function that's being declared, and are independent of the attributes of both the calling and receiving component. The parameters passed to an <aura:method>
are always dynamic insofar as they are passed in JavaScript, not declared in markup.
The example in the <aura:method>
documentation is illuminating. I'm adding a bit of outer structure here to clarify. In your scenario, the receiving component is the parent, and the calling component is the child, which has an attribute parent
to hold a reference to its parent component.
Receiving Component Markup
<aura:component ...>
<aura:method name="sampleMethod" action="{!c.doAction}"
description="Sample method with parameters">
<aura:attribute name="param1" type="String" default="parameter 1"/>
<aura:attribute name="param2" type="Object" />
</aura:method>
<aura:component>
Receiving Component Controller
({
doAction : function(cmp, event) {
var params = event.getParam('arguments');
if (params) {
var param1 = params.param1;
// add your code here
}
}
})
Calling Component Markup
<aura:component ...>
<aura:attribute name="parent" type="Aura.Component" />
<aura:attribute name="childAttribute" type="String" />
<aura:component>
Calling Component Controller
When we invoke the <aura:method>
, we specify the parameters as part of a JavaScript method call, rather than using Lightning value binds to establish them. It's more or less like any other method call. When we do this, we can source values from our (child) component's attributes, or from any other data source available in the context.
Here, we grab the value of a component attribute on the child and pass it as a parameter up to the parent, along with a static value.
myAction: function(component, event, handler) {
var someOtherComponentReference = component.get('v.parent');
someOtherComponentReference.sampleMethod(
component.get('v.childAttribute'),
{'foo': 'bar'}
);
}
See Calling Component Methods.