Skip to content

Commit 29bd1fa

Browse files
author
Alexander Vakrilov
authored
Merge pull request #562 from NativeScript/list-templates-snippets
LIstView template selector snippets
2 parents 81bae5f + a755044 commit 29bd1fa

File tree

4 files changed

+112
-14
lines changed

4 files changed

+112
-14
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- >> list-view-template-selector -->
2+
<ListView [items]="myItems" [itemTemplateSelector]="templateSelector">
3+
<template nsTemplateKey="header" let-item="item">
4+
<header-component [data]="item"></header-component>
5+
</template>
6+
7+
<template nsTemplateKey="item" let-item="item">
8+
<item-component [data]="item"></item-component>
9+
</template>
10+
</ListView>
11+
<!-- << list-view-template-selector -->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Component, Input, Injectable } from '@angular/core';
2+
3+
class DataItem {
4+
private static count = 0;
5+
public id: number;
6+
constructor(public name: string, public isHeader: boolean) {
7+
this.id = DataItem.count++;
8+
}
9+
}
10+
11+
@Component({
12+
selector: 'header-component',
13+
template: `<Label [text]='"HEADER: " +data.name'></Label>`
14+
})
15+
export class HeaderComponent {
16+
@Input() data: DataItem;
17+
}
18+
19+
@Component({
20+
selector: 'item-component',
21+
template: `<Label [text]='"ITEM: " + data.name'></Label>`
22+
})
23+
export class ItemComponent {
24+
@Input() data: DataItem;
25+
}
26+
27+
@Injectable()
28+
export class DataService {
29+
public getItems(): DataItem[] {
30+
const result = [];
31+
for (let headerIndex = 0; headerIndex < 10; headerIndex++) {
32+
result.push(new DataItem("Header " + headerIndex, true));
33+
34+
for (let i = 1; i < 10; i++) {
35+
result.push(new DataItem(`item ${headerIndex}.${i}`, false));
36+
}
37+
}
38+
return result;
39+
}
40+
}
41+
42+
@Component({
43+
moduleId: module.id,
44+
selector: 'list-test',
45+
templateUrl: "./template-selector.component.html"
46+
})
47+
// >> list-view-template-selector
48+
export class ListTemplateSelectorTest {
49+
public myItems: Array<DataItem>;
50+
51+
public templateSelector = (item: DataItem, index: number, items: any) => {
52+
return item.isHeader ? "header" : "item";
53+
}
54+
55+
constructor(private dataService: DataService) {
56+
}
57+
58+
ngOnInit() {
59+
this.myItems = this.dataService.getItems();
60+
}
61+
}
62+
// << list-view-template-selector

tests/app/snippets/navigation/page-outlet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {TestApp, registerTestApp} from "../../tests/test-app";
1+
import { TestApp, registerTestApp } from "../../tests/test-app";
22
import { ApplicationRef } from '@angular/core';
33
import { Router, NavigationStart, NavigationEnd } from "@angular/router";
44
// >> page-outlet-example
@@ -46,7 +46,7 @@ export class PageNavigationApp {
4646
NativeScriptRouterModule.forRoot(routes)
4747
]
4848
})
49-
export class PageNavigationAppModule {}
49+
export class PageNavigationAppModule { }
5050

5151
// >> (hide)
5252
function start_snippet_() {

tests/app/tests/snippets.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
//make sure you import mocha-config before @angular/core
2-
import {assert} from "./test-config";
2+
import { assert } from "./test-config";
33

4-
import {NavigationEnd, NavigationStart} from "@angular/router";
5-
import {Subscription} from "rxjs";
6-
import {TestApp, bootstrapTestApp, destroyTestApp} from "./test-app";
4+
import { NavigationEnd, NavigationStart } from "@angular/router";
5+
import { Subscription } from "rxjs";
6+
import { TestApp, bootstrapTestApp, destroyTestApp } from "./test-app";
77

8-
import {GestureComponent} from "../snippets/gestures.component";
9-
import {LayoutsComponent} from "../snippets/layouts.component";
10-
import {IconFontComponent} from "../snippets/icon-font.component";
8+
import { GestureComponent } from "../snippets/gestures.component";
9+
import { LayoutsComponent } from "../snippets/layouts.component";
10+
import { IconFontComponent } from "../snippets/icon-font.component";
1111

12-
import {PageNavigationApp} from "../snippets/navigation/page-outlet";
13-
import {NavigationApp} from "../snippets/navigation/router-outlet";
14-
import {FirstComponent, SecondComponent} from "../snippets/navigation/navigation-common";
15-
import {routes} from "../snippets/navigation/app.routes";
12+
import { PageNavigationApp } from "../snippets/navigation/page-outlet";
13+
import { NavigationApp } from "../snippets/navigation/router-outlet";
14+
import { FirstComponent, SecondComponent } from "../snippets/navigation/navigation-common";
15+
import { routes } from "../snippets/navigation/app.routes";
16+
import { HeaderComponent, ItemComponent, DataService, ListTemplateSelectorTest } from "../snippets/list-view/template-selector.component";
1617

17-
import {device, platformNames} from "platform";
18+
import { device, platformNames } from "platform";
1819
const IS_IOS = (device.os === platformNames.ios);
1920

2021
describe('Snippets', () => {
@@ -107,3 +108,27 @@ describe('Snippets Navigation', () => {
107108
});
108109
});
109110
});
111+
112+
describe('Snippets ListView', () => {
113+
let runningApp: any;
114+
115+
const cleanup = () => {
116+
if (runningApp) {
117+
destroyTestApp(runningApp);
118+
runningApp = null;
119+
}
120+
};
121+
122+
after(cleanup);
123+
124+
it("template selector", (done) => {
125+
bootstrapTestApp(ListTemplateSelectorTest, [DataService], null, [HeaderComponent, ItemComponent, ListTemplateSelectorTest])
126+
.then((app) => {
127+
setTimeout(() => {
128+
cleanup();
129+
done();
130+
}, 100);
131+
})
132+
.catch(err => done(err));
133+
});
134+
});

0 commit comments

Comments
 (0)