[SalesForce] Lightning – SObject Component

I want to build simple search component which can be configured for any Sobject, The code works fine for known standard sObject attributes like name, id, but there are other attributes which may vary for different object.

I'm wondering since lightning does client side rendering since its JS/HTML, I tried to access object properties as acc[0], acc[1], assuming 0 is Id, 1 is Name, but it didn't work. What is the alternate work around for dynamically accessing object properties?

Component code is below at high level,

<aura:attribute name="sobject" type="String" default="account"/>    <aura:attribute name="query" type="String" default="Select Id, Name From Account"/>
    <aura:attribute name="fields" type="String[]" default="['Id', 'Name']"/>
    <form>
        <ui:inputText class="form-control" label="Search" aura:id="searchField" value="" placeholder="enter search string"/>
        <ui:button label="Submit" press="{!c.searchSObjects}"/>
    </form>
    <ul class="list-group">
        <aura:iteration var="sobj" items="{!v.sobjects}">
            <li class="list-group-item"><a aura:id="sobjid" href="{!'/' + sobj.id}">{!sobj[0]} +", "+{!sobj[1]} </a></li>   
        </aura:iteration>
    </ul>

App config code

<aura:application >
    <link href='/resource/bootstrap/' rel="stylesheet"/>
    <h2>Account Search</h2>
    <DynamicSobjectSearch sobject="Account" query="Select Id, Name From Account" />
</aura:application>

Thanks

Best Answer

I strongly suspect that you're going to need to use JS remoting with an Aura enabled Apex controller to handle your queries for you as @PeterKnolle did in a blog Post titled Lightning Component Autocomplete. To make a controller aura enabled, you simply add @AuraEnabled annotation to any methods being called by your Aura components/applications.

You might want to also read a Developer Blog post by @DanielPeter titled Navigating to Reports & Records Using Lightning Component Events which references the former post.