Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 13
Registered: ‎10-01-2011
My Device: 9900
My Carrier: MTN
Accepted Solution

Accessing ListItemComponent instance from ListView onTriggered event

Hi,

 

I'm tearing my hair out with this!

 

I've got a ListItemComponent in my ListView, and I want to access the component instance in the onTriggered of my ListView. I've got the indexPath, but I cannot work out how to find the actual view.

 

Simple example:

 

    ListView {

      id: myListView

      dataModel: myDataModel

      layout: FlowListLayout {             }
      listItemComponents: [
        ListItemComponent {
          type: "imageItem"
            GalleryImages { }
        }

      ]

      onTriggered: {

        // this is easy

        var item = dataModel.data(indexPath);

        // BUT HOW DO I ACCESS THE instance of GalleryImages for the indexPath???

      }

  ...

 

I'm sure it's really simply, but have been bashing my head against it for 2 days!

 

C

Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: Accessing ListItemComponent instance from ListView onTriggered event

https://developer.blackberry.com/cascades/reference/bb__cascades__listview.html

 

your 'item' variable references the underlying data from the datamodel - it does not reference the list item visual (i.e. the ListItemComponent).

 

You will need to move your handler into the ListItemComponent - and probably change it to onTouch or whatever your GalleryImages component supports.

Contributor
Posts: 13
Registered: ‎10-01-2011
My Device: 9900
My Carrier: MTN

Re: Accessing ListItemComponent instance from ListView onTriggered event

 Thanks for the quick help!

 

I've been trying an example with code like this:

 

                ListItemComponent {
                    type: "item"
                    Container {
                        id: itemRoot
                        property alias secondText: secondLabel.text
                        Label {
                            text: ListItemData.name
                        }
                        Label {
                            id: secondLabel
                            text: "Second label"
                        }
                        onTouch: {
                            console.log("Pt1");
                            var it = itemRoot
                            var li = it.ListItem;
                            console.log("Pt2");
                        }
                    }
                }

 

But by the time I reach itemRoot.ListItem, I'm undefined. If I'm understanding this http://stackoverflow.com/questions/14357483/accessing-context-property-from-within-an-ontouch-method correctly, this should be working!

 

Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: Accessing ListItemComponent instance from ListView onTriggered event

[ Edited ]

itemRoot does not contain a list item - it is a Container.

Just access the ListItem directly in your code

var li = ListItem;

 

OR but an ID on your listitemcomponent

 

 ListItemComponent {
     type: "item"

    id: myItem

     

   Container {

 

    }

 

}