Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35c7c66

Browse files
author
Nedyalko Nikolov
committed
Added setupItemView event for Angular ListView.
1 parent d45b8f9 commit 35c7c66

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

‎src/nativescript-angular/directives/list-view-comp.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
HostListener,
1010
IterableDiffers,
1111
IterableDiffer,
12-
ChangeDetectorRef} from 'angular2/core';
12+
ChangeDetectorRef,
13+
EventEmitter,
14+
Output} from 'angular2/core';
1315
import {Observable as RxObservable} from 'rxjs'
1416
import {ListView} from 'ui/list-view';
1517
import {View} from 'ui/core/view';
@@ -18,6 +20,12 @@ import {ObservableArray} from 'data/observable-array';
1820
import {LayoutBase} from 'ui/layouts/layout-base';
1921
const NG_VIEW = "_ngViewRef";
2022

23+
interface SetupItemViewArgs {
24+
view: EmbeddedViewRef;
25+
data: any;
26+
index: number;
27+
}
28+
2129
@Component({
2230
selector: 'ListView',
2331
template: ``,
@@ -28,6 +36,8 @@ export class ListViewComponent {
2836
private _items: any;
2937
private _differ: IterableDiffer;
3038

39+
@Output() public setupItemView: EventEmitter<SetupItemViewArgs> = new EventEmitter<SetupItemViewArgs>();
40+
3141
@ContentChild(TemplateRef) itemTemplate: TemplateRef;
3242

3343
set items(value: any) {
@@ -82,6 +92,7 @@ export class ListViewComponent {
8292
viewRef.setLocal("index", index);
8393
viewRef.setLocal('even', (index % 2 == 0));
8494
viewRef.setLocal('odd', (index % 2 == 1));
95+
this.setupItemView.next({'view': viewRef, 'data': data, 'index': index});
8596
}
8697

8798
ngDoCheck() {

‎tests/app/tests/list-view-tests.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {assert} from "./test-config";
2+
import {
3+
ElementRef,
4+
Component
5+
} from 'angular2/core';
6+
import {TestApp} from "./test-app";
7+
8+
class DataItem {
9+
constructor(public id: number, public name: string) { }
10+
}
11+
12+
@Component({
13+
selector: 'list-view-setupItemView',
14+
template: `
15+
<StackLayout>
16+
<ListView height="200" [items]="myItems" (setupItemView)="onSetupItemView($event)">
17+
<template #item="item" #i="index" #odd="odd" #even="even">
18+
<StackLayout [class.odd]="odd" [class.even]="even">
19+
<Label [text]='"index: " + i'></Label>
20+
<Label [text]='"[" + item.id +"] " + item.name'></Label>
21+
</StackLayout>
22+
</template>
23+
</ListView>
24+
</StackLayout>
25+
`
26+
})
27+
export class TestListViewComponent {
28+
public myItems: Array<DataItem>;
29+
public counter: number;
30+
31+
constructor(public elementRef: ElementRef) {
32+
this.counter = 0;
33+
this.myItems = [];
34+
for (var i = 0; i < 2; i++) {
35+
this.myItems.push(new DataItem(i, "data item " + i));
36+
}
37+
}
38+
39+
onSetupItemView(args) {
40+
this.counter++;
41+
}
42+
}
43+
44+
describe.only('ListView-tests', () => {
45+
let testApp: TestApp = null;
46+
47+
before(() => {
48+
return TestApp.create().then((app) => {
49+
testApp = app;
50+
})
51+
});
52+
53+
after(() => {
54+
testApp.dispose();
55+
});
56+
57+
afterEach(() => {
58+
testApp.disposeComponents();
59+
});
60+
61+
it('setupItemView is called for every item', (done) => {
62+
return testApp.loadComponent(TestListViewComponent).then((componentRef) => {
63+
const componentRoot = componentRef.instance.elementRef.nativeElement;
64+
const component = componentRef.instance;
65+
const listView = componentRoot.getChildAt(0);
66+
67+
setTimeout(() => {
68+
assert.equal(component.counter, 2);
69+
done();
70+
}, 20);
71+
});
72+
});
73+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /