Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Last active July 20, 2017 13:25
Show Gist options
  • Save Sunil02kumar/465312ebec355ef032c955560c0fb484 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/465312ebec355ef032c955560c0fb484 to your computer and use it in GitHub Desktop.
Inheritance in Lightning Components
<aura:component controller="SK_AccountListController" extends="c:SK_CallToServerUtility">
<aura:attribute type="Object" name="returnedRecords" />
<aura:attribute type="string" name="searchValue" default="" />
<aura:set attribute="msg" value="SK_CallToServerUtility component is inherited in SK_AccountList Component"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div>
<div class="slds-align--absolute-center">
Inherited Component attribute (msg) value set by this component:<b>{!v.msg}</b>
</div>
<br/>
<div >
<lightning:input type="text" label="Enter Account Name" name="schval" value="{!v.searchValue}" aura:Id="schtxt"/>
<lightning:button label="Search Account" variant="brand" aura:Id="btn1" onclick="{!c.searchAccounts}"/>
</div>
<br/>
<div class="slds-align--absolute-center">
<table aura:id="accview" class="slds-table slds-table--bordered slds-table--cell-buffer" cellspacing="0" width="100%">
<thead>
<tr class="slds-text-title--caps">
<th scope="col">Account name</th>
<th scope="col">Account Type</th>
<th scope="col">Account Industry</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.returnedRecords}" var="rec">
<tr>
<td>{!rec.Name}</td>
<td>{!rec.Type}</td>
<td>{!rec.Industry}</td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
</div>
</aura:component>
public class SK_AccountListController {
@AuraEnabled
public static List<Account> findAccounts(string acname){
system.debug('********acname:'+acname);
List<Account> accList = new List<Account>();
string searchText= string.escapeSingleQuotes(acname);
searchText = '%' + searchText + '%';
string queryString = '';
if(acname != null && acname != ''){
queryString = queryString + 'select id,name,type,industry from Account where name Like : searchText order by LastModifiedDate DESC Limit 10';
}else{
queryString = queryString + 'select id,name,type,industry from Account order by LastModifiedDate DESC Limit 100';
}
try{
accList = database.query(queryString);
}catch(exception ex){
system.debug('Problem with SOQL Query:'+ ex.getMessage());
}
return accList;
}
}
({
doInit : function(component, event, helper) {
console.log('searchAccounts get called in doInit');
helper.callToServer(
component,
"c.findAccounts",
function(response) {
console.log(' Response in SK_AccountList doInit:'+ JSON.stringify(response));
var apexResponse = response;
component.set("v.returnedRecords", apexResponse);
},
{
acname: component.get("v.searchValue")
}
);
},
searchAccounts : function(component, event, helper) {
console.log('searchAccounts get called');
helper.callToServer(
component,
"c.findAccounts",
function(response) {
console.log(' Response in SK_AccountList searchAccounts:'+ JSON.stringify(response));
var apexResponse = response;
component.set("v.returnedRecords", apexResponse);
},
{
acname: component.get("v.searchValue")
}
);
}
})
<aura:component extensible="true">
<!-- below attribute value will be set by components which will inherit it-->
<aura:attribute name="msg" type="String" default=""/>
{!v.body}
</aura:component>
({
callToServer : function(component, method, callback, params) {
console.log('Calling callToServer function in SK_CallToServerUtility extensible Component');
var action = component.get(method);
if(params){
action.setParams(params);
}
//console.log(JSON.stringify(params));
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
//alert('Processed successfully at server');
callback.call(this,response.getReturnValue());
}else if(state === "ERROR"){
alert('Problem with connection. Please try again.');
}
});
$A.enqueueAction(action);
}
})
<aura:application extends="force:slds">
<c:SK_AccountList />
</aura:application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment