Skip to content

Instantly share code, notes, and snippets.

@dancinllama
Last active November 21, 2016 16:49
Show Gist options
  • Save dancinllama/83883a73bead6e3adb5e4d6a95d33baa to your computer and use it in GitHub Desktop.
Save dancinllama/83883a73bead6e3adb5e4d6a95d33baa to your computer and use it in GitHub Desktop.
Component for displaying Contact Name(s)
/**
* ContactsCtrl
* @description Apex and stuff..
* @author
* @date 11/21/2016
*/
public with sharing class ContactsCtrl {
@AuraEnabled
public static List<Contact> getContacts(){
return [Select Name From Contact Limit 5];
}
@AuraEnabled
public static Contact getCurrentContact(){
User u = [Select Contact.Name From User Where Id=:UserInfo.getUserId()];
return u.Contact;
}
}
({
doInit : function(component, event, helper) {
//Call action to get a list of contacts via Apex.
var action = component.get("c.getContacts");
action.setCallback(this,function(a){
if(a.getState() === "SUCCESS"){
component.set("v.contacts",a.getReturnValue());
}
});
$A.enqueueAction(action);
//Call action to get current contact's name.
var action2 = component.get("c.getCurrentContact");
action2.setCallback(this,function(a){
if(a.getState() === "SUCCESS"){
component.set("v.currentContact",a.getReturnValue());
}
});
$A.enqueueAction(action2);
}
})
<aura:component implements="forceCommunity:availableForAllPageTypes" controller="ContactsCtrl">
<aura:attribute name="contacts" type="Contact[]" />
<aura:attribute name="currentContact" type="Contact" />
<!-- example for displaying list of contacts -->
<aura:iteration items="{!v.contacts}" var="contact">
<ui:outputText value="{!contact.Name}" />
</aura:iteration>
<!-- example for display current contact's name -->
Hello, {!v.currentContact.Name}!
</aura:component>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment