File tree Expand file tree Collapse file tree 2 files changed +50
-8
lines changed Expand file tree Collapse file tree 2 files changed +50
-8
lines changed Original file line number Diff line number Diff line change 1
1
< script src ="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js "> </ script >
2
- < script src ="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4 .0.6/rx.lite .js "> </ script >
2
+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5 .0.0-rc.1/Rx.min .js "> </ script >
3
3
< script src ="https://unpkg.com/vue/dist/vue.js "> </ script >
4
4
< script src ="../vue-rx.js "> </ script >
5
5
@@ -29,18 +29,18 @@ <h1>{{ results.term }}</h1>
29
29
. filter ( function ( text ) {
30
30
return text . length > 2
31
31
} )
32
- . debounce ( 500 )
32
+ . debounceTime ( 500 )
33
33
. distinctUntilChanged ( )
34
- . flatMapLatest ( function ( term ) {
35
- return $ . ajax ( {
34
+ . switchMap ( function ( term ) {
35
+ return Rx . Observable . fromPromise ( $ . ajax ( {
36
36
url : 'http://en.wikipedia.org/w/api.php' ,
37
37
dataType : 'jsonp' ,
38
38
data : {
39
39
action : 'opensearch' ,
40
40
format : 'json' ,
41
41
search : term
42
42
}
43
- } ) . promise ( )
43
+ } ) . promise ( ) )
44
44
} )
45
45
// format the final data a little bit
46
46
. map ( function ( res ) {
@@ -60,7 +60,22 @@ <h1>{{ results.term }}</h1>
60
60
var vm = new Vue ( {
61
61
el : '#el' ,
62
62
data : {
63
+ a :5 ,
63
64
results : dataSource
64
65
}
65
- } )
66
+ } ) ;
67
+
68
+
69
+ setTimeout ( function ( ) {
70
+ vm . a = 17 ;
71
+ } , 5000 )
72
+
73
+ vm . $watchAsObservable ( 'a' )
74
+ . subscribe ( function ( val ) {
75
+ console . log ( 'stream value' , val ) ;
76
+ } , function ( err ) {
77
+ console . error ( err )
78
+ } , function ( ) {
79
+ console . log ( 'complete' ) ;
80
+ } )
66
81
</ script >
Original file line number Diff line number Diff line change 1
1
( function ( ) {
2
- function VueRx ( Vue ) {
2
+ function VueRx ( Vue , Rx ) {
3
3
var VueVersion = Number ( Vue . version && Vue . version . split ( '.' ) [ 0 ] )
4
4
var initHook = VueVersion && VueVersion > 1 ? 'beforeCreate' : 'init'
5
5
39
39
}
40
40
41
41
Vue . mixin ( mixin )
42
+
43
+
44
+ Vue . prototype . $watchAsObservable = function ( expOrFn , options ) {
45
+ var self = this ;
46
+
47
+ var obs$ = Rx . Observable . create ( function ( observer ) {
48
+ // Create function to handle old and new Value
49
+ function listener ( newValue , oldValue ) {
50
+ observer . next ( { oldValue : oldValue , newValue : newValue } ) ;
51
+ }
52
+
53
+ // Returns function which disconnects the $watch expression
54
+ var disposable ;
55
+ if ( Rx . Subscription ) { //Rx5
56
+ disposable = new Rx . Subscription ( self . $watch ( expOrFn , listener , options ) ) ;
57
+ } else { //Rx4
58
+ disposable = Rx . Disposable . create ( self . $watch ( expOrFn , listener , options ) ) ;
59
+ }
60
+
61
+ return disposable ;
62
+ } ) . publish ( ) . refCount ( ) ;
63
+
64
+ ( self . _rxHandles || ( self . _rxHandles = [ ] ) ) . push ( obs$ ) ;
65
+
66
+ return obs$ ;
67
+ }
68
+
42
69
}
43
70
44
71
// auto install
45
72
if ( typeof Vue !== 'undefined' ) {
46
- Vue . use ( VueRx )
73
+ Vue . use ( VueRx , Rx )
47
74
}
48
75
49
76
if ( typeof exports === 'object' && typeof module === 'object' ) {
You can’t perform that action at this time.
0 commit comments