Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Created January 22, 2018 05:51
Show Gist options
  • Save Sunil02kumar/6dd94460f61e452121f34fa92191641b to your computer and use it in GitHub Desktop.
Save Sunil02kumar/6dd94460f61e452121f34fa92191641b to your computer and use it in GitHub Desktop.
<aura:component controller="SK_DemoSF1EventsCmpController" implements="force:appHostable" >
<aura:attribute name="recList" type="List" />
<aura:attribute name="menu" type="List" default="View,Edit" description="Optional Menu Items"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:handler event="aura:waiting" action="{!c.showSpinner}"/>
<aura:handler event="aura:doneWaiting" action="{!c.hideSpinner}"/>
<div class="slds-align--absolute-center">
<lightning:spinner aura:id="spinner" variant="brand" size="large" class="slds=hide"/>
</div>
<table class="slds-table slds-table--bordered slds-table--cell-buffer">
<thead>
<tr class="slds-text-title--caps">
<th scope="col">Actions</th>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Industry</th>
</tr>
</thead>
<aura:iteration items="{!v.recList}" var="item">
<tr>
<td>
<lightning:buttonMenu iconName="utility:threedots" >
<aura:iteration items="{!v.menu}" var="menuItem">
<lightning:menuItem label="{!menuItem}" value="{!item.Id + '---' + menuItem}" onactive="{!c.onSelectMenuItem}"/>
</aura:iteration>
</lightning:buttonMenu>
</td>
<td> {!item.Name}</td>
<td> {!item.Type}</td>
<td> {!item.Industry}</td>
</tr>
</aura:iteration>
</table>
</aura:component>
<aura:application extends="force:slds">
<c:SK_DemoSF1EventsCmp />
</aura:application>
public class SK_DemoSF1EventsCmpController {
@auraEnabled
public static List<sobject> findRecords(){
return [Select id,Name,Type,Industry from Account Order by lastmodifiedDate DESC Limit 10];
}
}
({
doInit : function(component, event, helper){
var params ={}
helper.callToServer(
component,
"c.findRecords",
function(response){
component.set("v.recList",response);
//display message that records retrieved successfully by using toast
helper.raiseS1Event(
component,
event,
helper,
"event.force:showToast",
{
"title":"Success",
"type":"success",
"message" : "Retrieved Accounts Successfully.."
}
);
},
params
);
},
onSelectMenuItem : function(component, event, helper) {
var selectedOption = event.getSource().get('v.value');
console.log('******selectedOption:'+selectedOption);
var selectedrecordId = selectedOption.split('---')[0];
if(selectedOption.endsWith("View")){
helper.raiseS1Event(
component,
event,
helper,
"event.force:navigateToSObject",
{recordId: selectedrecordId}
);
}else if(selectedOption.endsWith("Edit")){
helper.raiseS1Event(
component,
event,
helper,
"event.force:editRecord",
{recordId: selectedrecordId}
);
}
},
showSpinner: function(component, event, helper) {
var spinner = component.find("spinner");
$A.util.removeClass(spinner, "slds-hide");
},
hideSpinner: function(component, event, helper) {
//alert('hideSpinner');
var spinner = component.find("spinner");
$A.util.addClass(spinner, "slds-hide");
}
})
({
callToServer : function(component, method, callback, params) {
var action = component.get(method);
if(params){
action.setParams(params);
}
console.log(JSON.stringify(params));
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
//alert('Processed successfully at server');
callback.call(this,response.getReturnValue());
}else if(state === "ERROR"){
alert('Problem with connection. Please try again.');
}
});
$A.enqueueAction(action);
},
raiseS1Event: function(component, event, helper, eventName, params) {
var evt = $A.get(eventName);
//checking if SF1 event exist or not. these events exist in lightning experience and Salesforce 1
if (evt) {
evt.setParams(params);
console.log('params for event:'+JSON.stringify(params));
evt.fire();
} else {
var msg = 'This feature ('+eventName+ ') is supported in Lightning Experience, Salesforce1, and Lightning communities.';
msg = msg + 'Please switch to lightning experience and create a lightning tab for SK_DemoSF1EventsCmp component to view SF1 events functionality';
alert(msg);
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment