Skip to content

Instantly share code, notes, and snippets.

@sfcure
Created July 27, 2018 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfcure/928e51b8a0674c5aa4964cf621788e1d to your computer and use it in GitHub Desktop.
Save sfcure/928e51b8a0674c5aa4964cf621788e1d to your computer and use it in GitHub Desktop.
How to use namespace in managed lightning components
<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>
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 ];
}
}
({
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);
},
})
({
// 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