AuraAttributes
Binding attribute in Application
<aura:application extends="force:slds">
<aura:attribute name="value" type="String" default="World"/>
Hello {!v.value}!
</aura:application>
O/p
Hello World!
Hello World example sending value from js to server as param and getting output.
Apex
public with sharing class helloWorldwithParam {
@AuraEnabled
public static String serverEcho(String firstName) {
return ('Hello from the server, ' + firstName);
}
}
Application
<!-- Adding classname of apexclass -->
<aura:application controller="helloWorldwithParam" extends="force:slds">
<aura:attribute name="firstName" type="String" default="world"/>
<lightning:button label="Call server" onclick="{!c.echo}"/>
</aura:application>
Controller
({
"echo" : function(cmp) {
// create a one-time use instance of the serverEcho action
// in the server-side controller
//Calling the method of apexclass
var action = cmp.get("c.serverEcho");
//setting the value as parameter in the server
action.setParams({ firstName : cmp.get("v.firstName") });
// Create a callback that is executed after
// the server-side action returns
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
// Alert the user with the value returned
// from the server
alert("From server: " + response.getReturnValue());
// You would typically fire a event here to trigger
// client-side notification that the server-side
// action is complete
}
else if (state === "INCOMPLETE") {
// do something
}
else if (state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log("Error message: " +
errors[0].message);
}
} else {
console.log("Unknown error");
}
}
});
// optionally set storable, abortable, background flag here
// A client-side action could cause multiple events,
// which could trigger other events and
// other server-side action calls.
// $A.enqueueAction adds the server-side action to the queue.
$A.enqueueAction(action);
}
})
O/p
From server: Hello from the server, world
Comments
Post a Comment