Created
April 30, 2018 19:49
-
-
Save sohalloran/760083169ca99819a660432b5e036710 to your computer and use it in GitHub Desktop.
*Using a Service Component Example App*
This file contains hidden or 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 > | |
<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; | |
} | |
}) |
This file contains hidden or 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 > | |
*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