Skip to content

Commit 402ded0

Browse files
author
Nedyalko Nikolov
committed
Example how NS support async pipe with RxObservable.
1 parent 856db41 commit 402ded0

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

Diff for: ng-sample/app/list-test-async.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Component, Input, ChangeDetectionStrategy } from 'angular2/core';
2+
import { Observable as RxObservable } from 'rxjs/Observable';
3+
import { Observable, WrappedValue } from 'data/observable';
4+
5+
class DataItem {
6+
constructor(public id: number, public name: string) { }
7+
}
8+
9+
@Component({
10+
selector: 'list-test-async',
11+
template: `
12+
<ListView [items]="myItems | async" (itemTap)="onItemTap($event)">
13+
<item-template>
14+
<template #item="item" #i="index" #odd="odd" #even="even">
15+
<StackLayout [class.odd]="odd" [class.even]="even">
16+
<Label [text]='"index: " + item.name'></Label>
17+
</StackLayout>
18+
</template>
19+
</item-template>
20+
</ListView>
21+
`,
22+
changeDetection: ChangeDetectionStrategy.OnPush
23+
})
24+
export class ListTestAsync {
25+
public myItems: RxObservable<Array<DataItem>>;
26+
27+
constructor() {
28+
var items = [];
29+
for (var i = 0; i < 3; i++) {
30+
items.push(new DataItem(i, "data item " + i));
31+
}
32+
33+
var subscr;
34+
this.myItems = RxObservable.create(subscriber => {
35+
subscr = subscriber;
36+
subscriber.next(WrappedValue.wrap(items));
37+
return function () {
38+
console.log("Unsubscribe called!!!");
39+
}
40+
});
41+
42+
let counter = 2;
43+
let intervalId = setInterval(() => {
44+
counter++;
45+
console.log("Adding " + counter + "-th item");
46+
items.push(new DataItem(counter, "data item " + counter));
47+
subscr.next(WrappedValue.wrap(items));
48+
}, 1000);
49+
50+
setTimeout(() => {
51+
clearInterval(intervalId);
52+
}, 15000);
53+
}
54+
55+
public onItemTap(args) {
56+
console.log("------------------------ ItemTapped: " + args.index);
57+
}
58+
}

Diff for: ng-sample/app/main-page.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {Page} from 'ui/page';
66
import {nativeScriptBootstrap} from './nativescript-angular/application';
77
// import {RendererTest} from './renderer-test';
88
//import {Benchmark} from './benchmark';
9-
import {ListTest} from './list-test';
9+
//import {ListTest} from './list-test';
10+
import {ListTestAsync} from './list-test-async';
1011

1112
export function createPage() {
1213
var page = new Page();
@@ -19,7 +20,8 @@ export function createPage() {
1920
console.log('BOOTSTRAPPING...');
2021
//nativeScriptBootstrap(Benchmark, []).then((appRef) => {
2122
// nativeScriptBootstrap(RendererTest, []).then((appRef) => {
22-
nativeScriptBootstrap(ListTest, []).then((appRef) => {
23+
//nativeScriptBootstrap(ListTest, []).then((appRef) => {
24+
nativeScriptBootstrap(ListTestAsync, []).then((appRef) => {
2325
profiling.stop('ng-bootstrap');
2426
console.log('ANGULAR BOOTSTRAP DONE.');
2527
}, (err) =>{

0 commit comments

Comments
 (0)