1

I am using following LWC wrapper in aura to get list views:

LWC:

import { LightningElement, wire, track, api } from 'lwc';
import { getListUi } from 'lightning/uiListApi';

export default class WireListView extends LightningElement {
    @api objectApiName;
    @api listViewApiName;
    @wire(getListUi, {
            objectApiName: '$objectApiName',
            listViewApiName: '$listViewApiName'
        })
    listView
    @api get result(){
        return this.listView;
    }
}

Aura:

<aura:handler name="change" value="{!v.myList}" action="{!c.listViewChange}"/>
<DecsOnD:wireListView
                aura:id="wireListView"
                listViewApiName="{!!empty(v.selectedView.NamespacePrefix)
                    ? v.selectedView.NamespacePrefix + '__' + v.selectedView.DeveloperName
                    : v.selectedView.DeveloperName}"
                objectApiName="{!v.selectedObject}"
                result="{!v.myList}"/>

I've got couple of problems:

  1. I need to change v.selectedView.DeveloperName couple for times so that actual record propagates. I suspect this due to how @api get reusult works - it returns previous record
  2. My change handler fires twice, which is sort of ok, but wondering why - I suspect because I'm passing two attributes?
  3. On init getListUi seems to retrieve a list of list views - which would be very useful if I can understand how it works exactly :) ATM I am using SOQL query, but in future I could use this instead.
  • "I need to change v.selectedView.DeveloperName couple for times"-- do you mean four times? Or something else? – sfdcfox Mar 31 '19 at 23:56
  • Meant couple of times. I have a dropdown with list view names that I need to click TWICE so my ui component gets the correct list of records. – dzh Apr 1 '19 at 0:09
2

I need to change v.selectedView.DeveloperName couple for times so that actual record propagates. I suspect this due to how @api get reusult works - it returns previous record

It's caching behavior plus some odd data propagation happening here. You'll want to fire a custom event for your aura component to listen to, and react accordingly. To do this, use a function with a backing property instead of using the listView property you're using now.

_listView;
@wire(getListUi, { objectApiName: '$objectApiName', listViewApiName: '$listViewApiName' })
listView({ error, data }) {
  if(data) {
    const listUpdate = new CustomEvent('listviewupdated', { data: data });
    this.dispatchEvent(listUpdate);
    this._listView = data;
  } else {
    // Choose if you wish to handle or ignore errors
  }
}
@api get result() {
  return this._listView;
}

You could even get clever here and dispatch one of two types of events, depending on if you get the list of list views, or if you get a single list view. This might make your logic in the aura component more legible as a bonus.

My change handler fires twice, which is sort of ok, but wondering why - I suspect because I'm passing two attributes?

Yes, this is expected, normal behavior. It shouldn't harm anything, though you might want to watch out for errors within your component. Notably, if given a choice, make sure you change objectApiName before you change listViewApiName.

On init getListUi seems to retrieve a list of list views - which would be very useful if I can understand how it works exactly :) ATM I am using SOQL query, but in future I could use this instead.

This is explained on the getListUi documentation. Basically, if objectApiName is not null, but listViewApiName is null, you'll get a list of list views. The structure of this is documented at the bottom of the getListUi doc page.

| improve this answer | |
  • Hi almost there... Just question wrt custom events - the documentation shows some magic one can grab data by collection name var filters = event.getParam('filters');. I am not seeing that... I've tried this const myarr = data; const listUpdate = new CustomEvent('listviewupdated', { detail: myarr }); - I do get event callback but data is not there - tried with getParam detail, data, myarr, etc... – dzh Apr 1 '19 at 0:51
  • @dzh According to the docs, it should work. I'll experiment with it after dinner, give me like an hour. – sfdcfox Apr 1 '19 at 0:54
  • No worries found the issue by looking at lwc recipes. Pretty sure docs are wrong. This works: const listUpdate = new CustomEvent('listviewupdated', { detail: {mydata : data} }); and then var filters = event.getParam('mydata'); – dzh Apr 1 '19 at 0:55
  • @dzh oh, cool. Time to ile a bug report, lol. – sfdcfox Apr 1 '19 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.