How to use xml-snippet as itemTemplate for dynamically created ListView


#1

Hi,

in our NativeScript we share code snippets between different pages, like the itemTemplate of a ListView:

<ListView items="{{ items}}">
        <ListView.itemTemplate>
            <myNameSpace:list-template/>
        </ListView.itemTemplate>
</ListView>

where list-template.xml contains the snippet for an itemTemplate, e.g.

<StackLayout>
         <Label text="{{ name }}" />
         ...
</StackLayout>

My question is how to do the exact same thing for a dynamically created ListView from JavaScript code.
Right now, I assign the template like that:

myListView.itemTemplate =
 `<StackLayout>
         <Label text="{{ name }}" />
</StackLayout>`;

But assigning the template this way is not very flexible, as I am already sharing the template from the XML file.
In Angular, I could use the templateURL, to refer to the XML file (or HTML), but I use Vanilla JS. Is there anything similar I could do?

I am thankful for any advice.


#2

At this moment; to my knowledge the only way to do this in PAN (Plain Awesome NativeScript) is to use my NativeScript-DynamicLoader plugin. It allows you to dynamically load in parts of the view screen.


#3

Hi Nathanael,

thanks for your response. I’ll have a look at your plugin :wink: