Skip to content

Commit 6775ba3

Browse files
committed
integrate VueRx w/ example event subscription
$ npm install vue-rx rxjs --save
1 parent 263a853 commit 6775ba3

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
99
},
1010
"dependencies": {
11+
"rxjs": "^5.5.2",
1112
"typescript": "^2.5.3",
1213
"vue": "^2.4.4",
14+
"vue-rx": "^5.0.0",
1315
"vuex": "^3.0.0"
1416
},
1517
"devDependencies": {

src/App.vue

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
import { Component, Vue, Watch } from 'vue-property-decorator'
5959
import { Getter, Mutation } from 'vuex-class'
6060
61+
import { Observable } from 'rxjs/Observable'
62+
import 'rxjs/add/observable/fromEvent'
63+
import 'rxjs/add/operator/map'
64+
import 'rxjs/add/operator/startWith'
65+
import 'rxjs/add/operator/shareReplay'
66+
6167
import Todo from './Todo'
6268
6369
Vue.filter('pluralize', (n: number): string => {
@@ -71,7 +77,14 @@ Vue.directive('todo-focus', (el, binding) => {
7177
})
7278
7379
@Component({
74-
name: 'app'
80+
name: 'app',
81+
subscriptions: {
82+
hashChange: Observable.fromEvent(window, 'hashchange')
83+
.map(() => window.location.hash)
84+
.startWith(window.location.hash)
85+
.shareReplay(1)
86+
,
87+
},
7588
})
7689
export default class App extends Vue {
7790
@Getter('all') todos
@@ -87,12 +100,6 @@ export default class App extends Vue {
87100
private editedTodo: Todo|null = null
88101
private beforeEditCache: string|null = null
89102
90-
constructor() {
91-
super()
92-
93-
window.addEventListener('hashchange', this.onHashChange)
94-
}
95-
96103
get filteredTodos(): Todo[] {
97104
return this.$store.getters[this.visibility]
98105
}
@@ -141,16 +148,18 @@ export default class App extends Vue {
141148
todo.title = this.beforeEditCache
142149
}
143150
144-
onHashChange() {
145-
const visibility = window.location.hash.replace(/#\/?/, '')
151+
created() {
152+
this.$observables.hashChange.subscribe((hash: string) => {
153+
const visibility = hash.replace(/#\/?/, '')
146154
147-
if (this.$store.getters[visibility]) {
148-
this.visibility = visibility
149-
} else {
150-
window.location.hash = ''
155+
if (this.$store.getters[visibility]) {
156+
this.visibility = visibility
157+
} else {
158+
window.location.hash = ''
151159
152-
this.visibility = 'all'
153-
}
160+
this.visibility = 'all'
161+
}
162+
})
154163
}
155164
}
156165
</script>

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex';
3+
import VueRx from 'vue-rx'
4+
import { Observable } from 'rxjs/Observable'
35

46
import App from './App.vue'
57
import TodoStore from './TodoStore'
68

79
Vue.use(Vuex)
10+
Vue.use(VueRx, {
11+
Observable
12+
})
813

914
new Vue({
10-
el: '#app',
11-
store: TodoStore(),
12-
render: h => h(App)
15+
el: '#app',
16+
store: TodoStore(),
17+
render: h => h(App),
1318
})

0 commit comments

Comments
 (0)