Skip to content

Instantly share code, notes, and snippets.

@sohalloran
Created April 30, 2018 19:49
Show Gist options
  • Save sohalloran/760083169ca99819a660432b5e036710 to your computer and use it in GitHub Desktop.
Save sohalloran/760083169ca99819a660432b5e036710 to your computer and use it in GitHub Desktop.
*Using a Service Component Example App*
<aura:component >
<aura:method name="add" action="{!c.add}" access="public">
<aura:attribute name="numbers" type="Double[]"/>
</aura:method>
<aura:method name="square" action="{!c.square}" access="public">
<aura:attribute name="number" type="Double"/>
</aura:method>
</aura:component>
// Controller //
({
add : function(component, event, helper) {
return helper.add(component, event);
},
square : function(component, event, helper) {
return helper.square(component, event);
}
})
// Helper //
({
add : function(component, event) {
var params = this.getParams(event);
return params.numbers.reduce(function(total, num){
return total + num;
});
},
square : function(component, event) {
var params = this.getParams(event);
return Math.pow(params.number,2);
},
getParams : function(event) {
var params = event.getParam('arguments');
if(!params) {
return [];
}
return params;
}
})
<aura:application >
*Using a Service Component Example App* <br/>
<aura:attribute name="number" type="double" default="2"/>
<c:MathSvc aura:id="mathSvc" />
<lightning:input type="string" label="number" value="{!v.number}"/>
<lightning:button label="Add" onclick="{!c.add}"/>
<lightning:button label="Square" onclick="{!c.square}"/>
</aura:application>
// Controller //
({
square : function(component, event, helper) {
component.set('v.number',
component.find('mathSvc').square(component.get('v.number')));
},
add : function(component, event, helper) {
component.set('v.number',
component.find('mathSvc')
.add(component.get('v.number')
.split(',')
.map(function(item) {
return parseFloat(item);
})));
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment