Last active
February 17, 2017 00:00
-
-
Save rjungemann/ec974947c87a5c4baf2177d0aba77447 to your computer and use it in GitHub Desktop.
RxJS and Vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"> | |
{{ message }} | |
<button v-on:click="increment">+</button> | |
<button v-on:click="decrement">-</button> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable.prototype.where = function (obj) { | |
var me = this; | |
return Rx.Observable.create(function (observer) { | |
me.subscribe({ | |
next: function (v) { | |
for (var key in obj) { | |
if (v[key] !== obj[key]) { | |
return; | |
} | |
} | |
observer.next(v); | |
}, | |
error: function (err) { observer.error(err) }, | |
complete: function () { observer.complete() } | |
}); | |
}); | |
} | |
var store = { | |
message: 1 | |
}; | |
var subject = new Rx.Subject(); | |
subject | |
.where({ name: 'increment' }) | |
.subscribe({ | |
next: function (e) { | |
store.message++; | |
} | |
}); | |
subject | |
.where({ name: 'decrement' }) | |
.subscribe({ | |
next: function (e) { | |
store.message--; | |
} | |
}); | |
var app = new Vue({ | |
el: '#app', | |
data: store, | |
methods: { | |
increment: function () { | |
subject.next({ name: 'increment' }); | |
}, | |
decrement: function () { | |
subject.next({ name: 'decrement' }); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.6/vue.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.1/Rx.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://reactivex.io/rxjs/manual/index.html
https://vuejs.org/v2/guide/instance.html