1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import Cursor from './cursor'
- export default {
- /**
- * Patch the instance's data function so that we can
- * directly bind to cursors in the `data` option.
- */
- init () {
- const dataFn = this.$options.data
- if (dataFn) {
- this.$options.data = () => {
- const raw = dataFn()
- Object.keys(raw).forEach(key => {
- const val = raw[key]
- if (val instanceof Cursor) {
- raw[key] = val.get()
- if (val.cb) {
- throw new Error(
- '[vue-store] A vue-store can only be subscribed to once.'
- )
- }
- val.subscribe(value => {
- this[key] = value
- })
- if (!this._vue_store_cursors) {
- this._vue_store_cursors = []
- }
- this._vue_store_cursors.push(val)
- }
- })
- return raw
- }
- }
- },
- /**
- * Dispose cursors owned by this instance.
- */
- beforeDestroy () {
- if (this._vue_store_cursors) {
- this._vue_store_cursors.forEach(c => c.dispose())
- }
- }
- }
|