Skip to content

Instantly share code, notes, and snippets.

@madmax983
Last active October 28, 2016 09:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madmax983/44f3ce76ffc226730a1934438a20a48a to your computer and use it in GitHub Desktop.
Save madmax983/44f3ce76ffc226730a1934438a20a48a to your computer and use it in GitHub Desktop.
Fun with Lightning Data Service
<aura:component >
<aura:attribute name="recordIds"
type="List"
access="public"
description="An array of Ids"/>
<aura:attribute name="records"
type="Map"
description="The records!"
default="{}"/>
<aura:attribute name="errors"
type="Map"
description="The errors!"
default="{}"/>
<aura:dependency resource="markup://force:recordPreview" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<lightning:button aura:id="logRecords"
label="Console.log records"
onclick="{!c.logRecords}" />
{!v.body}
</aura:component>
({
doInit : function(component, event, helper) {
var recordIdList = component.get("v.recordIds");
for(var i = 0; i < recordIdList.length; i++){
var nestedRecordString = "v.records." + recordIdList[i];
var nestedErrorString = "v.errors." + recordIdList[i];
$A.createComponent(
"force:recordPreview",
{
"aura:id": recordIdList[i],
"layoutType": "FULL",
"recordId": recordIdList[i],
"targetRecord": component.getReference(nestedRecordString),
"targetError": component.getReference(nestedErrorString)
},
function(newRecordPreview, status, errorMessage){
if (status === "SUCCESS") {
var body = component.get("v.body");
body.push(newRecordPreview);
component.set("v.body", body);
}
else if (status === "INCOMPLETE") {
console.log("No response from server or client is offline.")
// Show offline error
}
else if (status === "ERROR") {
console.log("Error: " + errorMessage);
// Show error message
}
}
);
}
},
logRecords: function(component, event, helper){
var recordIdList = component.get("v.recordIds");
for(var i = 0; i < recordIdList.length; i++){
var nestedRecordString = "v.records." + recordIdList[i];
console.log(component.get(nestedRecordString));
}
}
})
@madmax983
Copy link
Author

Lightning Data Service is only exposed in Lightning Experience and Salesforce1, so make sure that is where your component lives. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_considerations.htm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment