Created
July 27, 2018 14:22
-
-
Save sfcure/928e51b8a0674c5aa4964cf621788e1d to your computer and use it in GitHub Desktop.
How to use namespace in managed lightning components
This file contains 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 controller="NamespaceDemoCompController" implements="force:appHostable,flexipage:availableForRecordHome,force:hasRecordId" access="global" > | |
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/> | |
<aura:attribute name="namespace" type="String"/> | |
<aura:attribute name="totalAmount" type="Decimal"/> | |
Total Amount : {!v.totalAmount} | |
</aura:component> |
This file contains 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
public class NamespaceDemoCompController { | |
public NamespaceDemoCompController() {} | |
@AuraEnabled | |
public static String getNamespaceName() { | |
String nameSpacePrefix; | |
ApexClass cs =[select NamespacePrefix from ApexClass where Name ='NamespaceDemoCompController']; | |
if(cs.NamespacePrefix != null){ | |
nameSpacePrefix = cs.NamespacePrefix+'__'; | |
} | |
else{ | |
nameSpacePrefix = ''; | |
} | |
return nameSpacePrefix; | |
} | |
@AuraEnabled | |
public static List<Expense__c> getExpenses() { | |
return [SELECT Id,Name, Client__c, Date__c, Amount__c FROM Expense__c ]; | |
} | |
} |
This file contains 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
({ | |
doInit : function(component, event, helper) { | |
//call apex class method | |
var action = component.get('c.getNamespaceName'); | |
action.setCallback(this,function(response){ | |
//store state of response | |
var state = response.getState(); | |
if (state === "SUCCESS") { | |
//set response value in namespace attribute on component | |
component.set('v.namespace', response.getReturnValue()); | |
helper.getTotalAmount( component ); | |
} | |
}); | |
$A.enqueueAction(action); | |
}, | |
}) |
This file contains 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
({ | |
// Fetch the expenses from the Apex controller | |
// Calculate total amount here, it's just for demo purpose where we can see the use of namespace | |
getTotalAmount: function(component) { | |
var action = component.get('c.getExpenses'); | |
// Set up the callback | |
var self = this; | |
action.setCallback(this, function(actionResult) { | |
let expenseList = actionResult.getReturnValue(); | |
let totalAmount = 0; | |
let namespace = component.get('v.namespace'); | |
for(let i=0; i<expenseList.length; i++ ) { | |
totalAmount += expenseList[i][namespace + 'Amount__c']; | |
} | |
component.set('v.totalAmount', totalAmount); | |
}); | |
$A.enqueueAction(action); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment