Skip to content

Instantly share code, notes, and snippets.

@yasarshaikh
Created December 30, 2017 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasarshaikh/8edea8850bced036bec925a1d6f6db47 to your computer and use it in GitHub Desktop.
Save yasarshaikh/8edea8850bced036bec925a1d6f6db47 to your computer and use it in GitHub Desktop.
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
<aura:attribute name="boat" type="Boat__c" access="public"/>
<!-- <aura:attribute name="BoatReview" type="Object" access="private"/> -->
<aura:attribute name="boatReview" type="BoatReview__c" access="private"/>
<aura:attribute name="newBoatReview" type="Object" />
<aura:attribute name="recordError" type="String" access="private"/>
<aura:registerEvent name="BoatReviewAdded" type="c:BoatReviewAdded" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<force:recordData aura:id="service"
layoutType="FULL"
fields="Id, Name, Comment__c, Boat__c, Rating__c"
targetFields="{!v.boatReview}"
targetError="{!v.recordError}"
recordUpdated="{! c.onRecordUpdated }" />
<!-- Display Lightning Data Service errors -->
<aura:if isTrue="{!not(empty(v.recordError))}">
<div class="recordError">
<ui:message title="Error" severity="error" closable="true">
{!v.recordError}
</ui:message>
</div>
</aura:if>
<!-- Display the new contact form -->
<div class="slds-form slds-form_stacked">
<lightning:input aura:id="title" name="title" label="Title"
value="{! v.boatReview.Name }" required="true"/>
<lightning:inputRichText aura:id="Comment" value="{! v.boatReview.Comment__c }" disabledCategories="FORMAT_FONT"/>
<lightning:button label="Submit" onclick="{!c.onSave }"
iconName="utility:save" />
</div>
</aura:component>
({
doInit : function(component, event, helper) {
helper.onInit(component, event, helper);
},
onSave : function(component, event, helper) {
component.find("service").saveRecord($A.getCallback(function (saveResult){
if (saveResult.state == 'ERROR')
{
console.log('Error saving review record: ' + component.get('v.recordError'));
}
else {
var toastEvent = $A.get('e.force:showToast');
if (toastEvent) {
toastEvent.setParams({
title: 'Success!',
message: 'Your review has been saved successfully'
});
toastEvent.fire();
} else {
alert (message);
}
helper.onInit(component, event, helper);
component.getEvent('BoatReviewAdded').fire();
}
}));
},
onRecordUpdated: function(component, event, helper){
}
})
({
onInit : function(component, event, helper) {
var recordTypeId = null;
var skipCache = false;
var boatid = component.get('v.boat.Id');
component.find("service").getNewRecord(
'BoatReview__c',
recordTypeId,
skipCache,
$A.getCallback(function() {
var review = component.get('v.boatReview');
var error = component.get('v.recordError');
if (error || !review) {
console.log('Error initalizing review template: ' + error);
} else {
review.Boat__c = component.get('v.boat').Id;
}
})
);
},
alertSuccess: function(message) {
var toastEvent = $A.get('e.force:showToast');
if (toastEvent) {
toastEvent.setParams({
title: 'Success!',
message: message
});
toastEvent.fire();
} else {
alert (message);
}
}
})
<aura:component implements="flexipage:availableForAllPageTypes" access="global">
<aura:attribute name="boat" type="Boat__c" />
<aura:attribute name="id" type="Id" />
<aura:attribute access="private" name="selectedTabId" type="String"/>
<aura:handler event="c:BoatSelected" action="{! c.onBoatSelected }"/>
<aura:handler event="c:BoatReviewAdded" name="BoatReviewAdded" action="{! c.onBoatReviewAdded }" phase="capture"/>
<force:recordData aura:id="service"
recordId="{!v.id}"
layoutType="FULL"
fields="Id, Name, Description__c, Price__c, Length__c, Contact__r.Name, Contact__r.Email, Contact__r.HomePhone, BoatType__r.Name, Picture__c"
targetFields="{!v.boat}"
recordUpdated="{! c.onRecordUpdated }"
/>
<aura:if isTrue="{! v.boat != undefined }">
<lightning:tabset aura:id="boatTabSet" selectedTabId="{!v.selectedTabId}">
<lightning:tab label="Details" id="tabDetails">
<c:BoatDetail boat="{! v.boat }" />
</lightning:tab>
<lightning:tab label="Reviews" id="Reviews">
</lightning:tab>
<lightning:tab label="Add Review" id="tabAddReview">
<c:AddBoatReview boat="{! v.boat }" />
</lightning:tab>
</lightning:tabset>
</aura:if>
</aura:component>
({
onBoatSelected : function(component, event, helper) {
var boat = event.getParam('boat');
component.set("v.boat" , boat);
component.set("v.id" , component.get("v.boat.Id")) ;
component.find('service').reloadRecord();
},
onRecordUpdated : function(component, event, helper) {
},
onBoatReviewAdded : function(component, event, helper) {
component.set('v.selectedTabId', 'Reviews');
}
})
@nanabody
Copy link

nanabody commented Nov 19, 2018

onBoatReviewAdded : function(component, event, helper) { component.set('v.selectedTabId', 'boatreviewtab'); }

@XiCode633
Copy link

Hi thanks for the sharing, your onSave(component, event, helper) method in AddBoatReview.cmp helped me successfully initialize BoatReview record. I was stuck in this problem for hours.

However, I'm having a hard time understanding why using component.find("service").saveRecord($A.getCallback(function (saveResult){})) , instead of component.find("service").saveRecord(function (saveResult){}). And this seems like to be why my code didn't work.

Many thanks in advance :)

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