|
53 | 53 | Vue.mixin({ |
54 | 54 | created: function init () { |
55 | 55 | var vm = this |
| 56 | + var domStreams = vm.$option.domStreams |
| 57 | + if (domStreams) { |
| 58 | + domStreams.forEach(function (key) { |
| 59 | + vm[key] = new Rx.Subject() |
| 60 | + }) |
| 61 | + } |
| 62 | + |
56 | 63 | var obs = vm.$options.subscriptions |
57 | 64 | if (typeof obs === 'function') { |
58 | 65 | obs = obs.call(vm) |
59 | 66 | } |
60 | | - if (!obs) return |
61 | | - vm.$observables = {} |
62 | | - vm._obSubscriptions = [] |
63 | | - Object.keys(obs).forEach(function (key) { |
64 | | - defineReactive(vm, key, undefined) |
65 | | - var ob = vm.$observables[key] = obs[key] |
66 | | - if (!isObservable(ob)) { |
67 | | - warn( |
68 | | - 'Invalid Observable found in subscriptions option with key "' + key + '".', |
69 | | - vm |
70 | | - ) |
71 | | - return |
72 | | - } |
73 | | - vm._obSubscriptions.push(obs[key].subscribe(function (value) { |
74 | | - vm[key] = value |
75 | | - })) |
76 | | - }) |
| 67 | + if (obs) { |
| 68 | + vm.$observables = {} |
| 69 | + vm._obSubscriptions = [] |
| 70 | + Object.keys(obs).forEach(function (key) { |
| 71 | + defineReactive(vm, key, undefined) |
| 72 | + var ob = vm.$observables[key] = obs[key] |
| 73 | + if (!isObservable(ob)) { |
| 74 | + warn( |
| 75 | + 'Invalid Observable found in subscriptions option with key "' + key + '".', |
| 76 | + vm |
| 77 | + ) |
| 78 | + return |
| 79 | + } |
| 80 | + vm._obSubscriptions.push(obs[key].subscribe(function (value) { |
| 81 | + vm[key] = value |
| 82 | + })) |
| 83 | + }) |
| 84 | + } |
77 | 85 | }, |
78 | 86 | beforeDestroy: function () { |
79 | 87 | if (this._obSubscriptions) { |
|
157 | 165 | if (!hasRx()) { |
158 | 166 | return |
159 | 167 | } |
160 | | - var streamName = binding.arg |
161 | | - var vmStream = vnode.context[streamName] || vnode.context.$observables[streamName] |
162 | | - var eventNames = Object.keys(binding.modifiers) |
163 | | - |
164 | | - if (isSubject(vmStream)) { |
165 | | - var onNext = (vmStream.next || vmStream.onNext).bind(vmStream) // Rx4 Rx5 |
166 | | - el.vStreamData = binding.value |
167 | | - el._obs$ = eventNames.map(function (evtName) { |
168 | | - return Rx.Observable.fromEvent(el, evtName) |
169 | | - .subscribe(function (evt) { |
170 | | - onNext({ |
171 | | - event: evt, |
172 | | - data: el.vStreamData // Not using binding.value for data updating reason |
173 | | - }) |
174 | | - }) |
| 168 | + var event = binding.arg |
| 169 | + var stream = binding.value |
| 170 | + var streamName = binding.expression |
| 171 | + |
| 172 | + if (isSubject(stream)) { |
| 173 | + var onNext = (stream.next || stream.onNext).bind(stream) // Rx4 Rx5 |
| 174 | + el._ob$ = Rx.Observable.fromEvent(el, event).subscribe(function (evt) { |
| 175 | + onNext({ event: evt }) |
175 | 176 | }) |
176 | 177 | } else { |
177 | 178 | warn( |
178 | 179 | 'Invalid Subject found in directive with key "' + streamName + '".' + |
179 | | - 'Please declare ' + streamName + ' as an new Rx.Subject' |
| 180 | + streamName + ' should be an instance of Rx.Subject', |
| 181 | + vnode.context |
180 | 182 | ) |
181 | 183 | } |
182 | 184 | }, |
183 | | - update: function (el, binding, vnode) { |
184 | | - el.vStreamData = binding.value |
185 | | - }, |
186 | 185 | unbind: function (el, binding, vnode) { |
187 | | - if (Array.isArray(el._obs$)) { |
188 | | - el._obs$.forEach(function (ob) { |
189 | | - unsub(ob) |
190 | | - }) |
| 186 | + if (el._ob$) { |
| 187 | + unsub(el._ob$) |
191 | 188 | } |
192 | 189 | } |
193 | 190 | }) |
|
0 commit comments