|
| 1 | +(function (root, factory) { |
| 2 | + if (typeof exports === 'object' && typeof module === 'object') |
| 3 | + module.exports = factory() |
| 4 | + else if (typeof define === 'function' && define.amd) |
| 5 | + define([], factory) |
| 6 | + else if (typeof exports === 'object') |
| 7 | + exports["VueFire"] = factory() |
| 8 | + else |
| 9 | + root["VueFire"] = factory() |
| 10 | +})(this, function () { |
| 11 | + |
| 12 | + // late bind |
| 13 | + var Vue |
| 14 | + |
| 15 | + /** |
| 16 | + * Check if a value is an object. |
| 17 | + * |
| 18 | + * @param {*} val |
| 19 | + * @return {boolean} |
| 20 | + */ |
| 21 | + function isObject (val) { |
| 22 | + return Object.prototype.toString.call(val) === '[object Object]' |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Convert firebase snapshot into a bindable data record. |
| 27 | + * |
| 28 | + * @param {FirebaseSnapshot} snapshot |
| 29 | + * @return {Object} |
| 30 | + */ |
| 31 | + function createRecord (snapshot) { |
| 32 | + var value = snapshot.val() |
| 33 | + var res = value && typeof value === 'object' |
| 34 | + ? value |
| 35 | + : { _value: value } |
| 36 | + res._key = snapshot.key() |
| 37 | + return res |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Find the index for an object with given key. |
| 42 | + * |
| 43 | + * @param {array} array |
| 44 | + * @param {string} key |
| 45 | + * @return {number} |
| 46 | + */ |
| 47 | + function indexForKey (array, key) { |
| 48 | + for (var i = 0; i < array.length; i++) { |
| 49 | + if (array[i]._key === key) { |
| 50 | + return i |
| 51 | + } |
| 52 | + } |
| 53 | + return -1 |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Bind a firebase data source to a key on a vm. |
| 58 | + * |
| 59 | + * @param {Vue} vm |
| 60 | + * @param {object} data |
| 61 | + * @param {string} key |
| 62 | + * @param {object} source |
| 63 | + */ |
| 64 | + function bind (vm, data, key, source) { |
| 65 | + if (!isObject(source)) { |
| 66 | + throw new Error('Invalid Firebase binding source'); |
| 67 | + } |
| 68 | + var asArray = false |
| 69 | + var cancelCallback = null |
| 70 | + // check { source, asArray, cancelCallback } syntax |
| 71 | + if (isObject(source.source)) { |
| 72 | + asArray = source.asArray |
| 73 | + cancelCallback = source.cancelCallback |
| 74 | + source = source.source |
| 75 | + } |
| 76 | + // get the original ref for possible queries |
| 77 | + var ref = source |
| 78 | + if (typeof source.ref === 'function') { |
| 79 | + ref = source.ref() |
| 80 | + } |
| 81 | + vm.$firebaseRefs[key] = ref |
| 82 | + vm._firebaseSources[key] = source |
| 83 | + // bind based on initial value type |
| 84 | + if (asArray) { |
| 85 | + bindAsArray(vm, data, key, source, cancelCallback) |
| 86 | + } else { |
| 87 | + bindAsObject(vm, data, key, source, cancelCallback) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Bind a firebase data source to a key on a vm as an Array. |
| 93 | + * |
| 94 | + * @param {Vue} vm |
| 95 | + * @param {object} data |
| 96 | + * @param {string} key |
| 97 | + * @param {object} source |
| 98 | + * @param {function|null} cancelCallback |
| 99 | + */ |
| 100 | + function bindAsArray (vm, data, key, source, cancelCallback) { |
| 101 | + var array = data[key] = [] |
| 102 | + |
| 103 | + var onAdd = source.on('child_added', function (snapshot, prevKey) { |
| 104 | + var index = prevKey ? indexForKey(array, prevKey) + 1 : 0 |
| 105 | + array.splice(index, 0, createRecord(snapshot)) |
| 106 | + }, cancelCallback) |
| 107 | + |
| 108 | + var onRemove = source.on('child_removed', function (snapshot) { |
| 109 | + var index = indexForKey(array, snapshot.key()) |
| 110 | + array.splice(index, 1) |
| 111 | + }, cancelCallback) |
| 112 | + |
| 113 | + var onChange = source.on('child_changed', function (snapshot) { |
| 114 | + var index = indexForKey(array, snapshot.key()) |
| 115 | + array.splice(index, 1, createRecord(snapshot)) |
| 116 | + }, cancelCallback) |
| 117 | + |
| 118 | + var onMove = source.on('child_moved', function (snapshot, prevKey) { |
| 119 | + var index = indexForKey(array, snapshot.key()) |
| 120 | + var record = array.splice(index, 1)[0] |
| 121 | + var newIndex = prevKey ? indexForKey(array, prevKey) + 1 : 0 |
| 122 | + array.splice(newIndex, 0, record) |
| 123 | + }, cancelCallback) |
| 124 | + |
| 125 | + vm._firebaseListeners[key] = { |
| 126 | + child_added: onAdd, |
| 127 | + child_removed: onRemove, |
| 128 | + child_changed: onChange, |
| 129 | + child_moved: onMove |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Bind a firebase data source to a key on a vm as an Object. |
| 135 | + * |
| 136 | + * @param {Vue} vm |
| 137 | + * @param {object} data |
| 138 | + * @param {string} key |
| 139 | + * @param {Object} source |
| 140 | + * @param {function|null} cancelCallback |
| 141 | + */ |
| 142 | + function bindAsObject (vm, data, key, source, cancelCallback) { |
| 143 | + data[key] = {} |
| 144 | + var cb = source.on('value', function (snapshot) { |
| 145 | + vm[key] = snapshot.val() |
| 146 | + }, cancelCallback) |
| 147 | + vm._firebaseListeners[key] = { value: cb } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Unbind a firebase-bound key from a vm. |
| 152 | + * |
| 153 | + * @param {Vue} vm |
| 154 | + * @param {string} key |
| 155 | + */ |
| 156 | + function unbind (vm, key) { |
| 157 | + var source = vm._firebaseSources[key] |
| 158 | + var listeners = vm._firebaseListeners[key] |
| 159 | + for (var event in listeners) { |
| 160 | + source.off(event, listeners[event]) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + var VueFireMixin = { |
| 165 | + init: function () { |
| 166 | + var bindings = this.$options.firebase |
| 167 | + if (!bindings) return |
| 168 | + this.$firebaseRefs = Object.create(null) |
| 169 | + this._firebaseSources = Object.create(null) |
| 170 | + this._firebaseListeners = Object.create(null) |
| 171 | + // wrap data fn |
| 172 | + var vm = this |
| 173 | + var getData = this.$options.data |
| 174 | + this.$options.data = function () { |
| 175 | + var data = getData() |
| 176 | + for (var key in bindings) { |
| 177 | + bind(vm, data, key, bindings[key]) |
| 178 | + } |
| 179 | + return data |
| 180 | + } |
| 181 | + }, |
| 182 | + beforeDestroy: function () { |
| 183 | + for (var key in this.$firebaseRefs) { |
| 184 | + unbind(this, key) |
| 185 | + } |
| 186 | + this.$firebaseRefs = |
| 187 | + this._firebaseSources = |
| 188 | + this._firebaseListeners = null |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Install function passed to Vue.use() in manual installation. |
| 194 | + * |
| 195 | + * @param {function} _Vue |
| 196 | + */ |
| 197 | + function install (_Vue) { |
| 198 | + Vue = _Vue |
| 199 | + Vue.mixin(VueFireMixin) |
| 200 | + } |
| 201 | + |
| 202 | + // auto install |
| 203 | + if (typeof window !== 'undefined' && window.Vue) { |
| 204 | + install(window.Vue) |
| 205 | + } |
| 206 | + |
| 207 | + return install |
| 208 | +}) |
0 commit comments