1
+ ( function webpackUniversalModuleDefinition ( 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
+ return /******/ ( function ( modules ) { // webpackBootstrap
12
+ /******/ // The module cache
13
+ /******/ var installedModules = { } ;
14
+
15
+ /******/ // The require function
16
+ /******/ function __webpack_require__ ( moduleId ) {
17
+
18
+ /******/ // Check if module is in cache
19
+ /******/ if ( installedModules [ moduleId ] )
20
+ /******/ return installedModules [ moduleId ] . exports ;
21
+
22
+ /******/ // Create a new module (and put it into the cache)
23
+ /******/ var module = installedModules [ moduleId ] = {
24
+ /******/ exports : { } ,
25
+ /******/ id : moduleId ,
26
+ /******/ loaded : false
27
+ /******/ } ;
28
+
29
+ /******/ // Execute the module function
30
+ /******/ modules [ moduleId ] . call ( module . exports , module , module . exports , __webpack_require__ ) ;
31
+
32
+ /******/ // Flag the module as loaded
33
+ /******/ module . loaded = true ;
34
+
35
+ /******/ // Return the exports of the module
36
+ /******/ return module . exports ;
37
+ /******/ }
38
+
39
+
40
+ /******/ // expose the modules object (__webpack_modules__)
41
+ /******/ __webpack_require__ . m = modules ;
42
+
43
+ /******/ // expose the module cache
44
+ /******/ __webpack_require__ . c = installedModules ;
45
+
46
+ /******/ // __webpack_public_path__
47
+ /******/ __webpack_require__ . p = "" ;
48
+
49
+ /******/ // Load entry module and return exports
50
+ /******/ return __webpack_require__ ( 0 ) ;
51
+ /******/ } )
52
+ /************************************************************************/
53
+ /******/ ( [
54
+ /* 0 */
55
+ /***/ function ( module , exports ) {
56
+
57
+ var Vue // late binding
58
+
59
+ /**
60
+ * Returns the key of a Firebase snapshot across SDK versions.
61
+ *
62
+ * @param {FirebaseSnapshot } snapshot
63
+ * @return {string|null }
64
+ */
65
+ function _getKey ( snapshot ) {
66
+ return typeof snapshot . key === 'function'
67
+ ? snapshot . key ( )
68
+ : snapshot . key
69
+ }
70
+
71
+ /**
72
+ * Returns the original reference of a Firebase reference or query across SDK versions.
73
+ *
74
+ * @param {FirebaseReference|FirebaseQuery } refOrQuery
75
+ * @return {FirebaseReference }
76
+ */
77
+ function _getRef ( refOrQuery ) {
78
+ if ( typeof refOrQuery . ref === 'function' ) {
79
+ refOrQuery = refOrQuery . ref ( )
80
+ } else if ( typeof refOrQuery . ref === 'object' ) {
81
+ refOrQuery = refOrQuery . ref
82
+ }
83
+
84
+ return refOrQuery
85
+ }
86
+
87
+ /**
88
+ * Check if a value is an object.
89
+ *
90
+ * @param {* } val
91
+ * @return {boolean }
92
+ */
93
+ function isObject ( val ) {
94
+ return Object . prototype . toString . call ( val ) === '[object Object]'
95
+ }
96
+
97
+ /**
98
+ * Convert firebase snapshot into a bindable data record.
99
+ *
100
+ * @param {FirebaseSnapshot } snapshot
101
+ * @return {Object }
102
+ */
103
+ function createRecord ( snapshot ) {
104
+ var value = snapshot . val ( )
105
+ var res = isObject ( value )
106
+ ? value
107
+ : { '.value' : value }
108
+ res [ '.key' ] = _getKey ( snapshot )
109
+ return res
110
+ }
111
+
112
+ /**
113
+ * Find the index for an object with given key.
114
+ *
115
+ * @param {array } array
116
+ * @param {string } key
117
+ * @return {number }
118
+ */
119
+ function indexForKey ( array , key ) {
120
+ for ( var i = 0 ; i < array . length ; i ++ ) {
121
+ if ( array [ i ] [ '.key' ] === key ) {
122
+ return i
123
+ }
124
+ }
125
+ /* istanbul ignore next */
126
+ return - 1
127
+ }
128
+
129
+ /**
130
+ * Bind a firebase data source to a key on a vm.
131
+ *
132
+ * @param {Vue } vm
133
+ * @param {string } key
134
+ * @param {object } source
135
+ */
136
+ function bind ( vm , key , source ) {
137
+ var asObject = false
138
+ var cancelCallback = null
139
+ // check { source, asArray, cancelCallback } syntax
140
+ if ( isObject ( source ) && source . hasOwnProperty ( 'source' ) ) {
141
+ asObject = source . asObject
142
+ cancelCallback = source . cancelCallback
143
+ source = source . source
144
+ }
145
+ if ( ! isObject ( source ) ) {
146
+ throw new Error ( 'VueFire: invalid Firebase binding source.' )
147
+ }
148
+ var ref = _getRef ( source )
149
+ vm . $firebaseRefs [ key ] = ref
150
+ vm . _firebaseSources [ key ] = source
151
+ // bind based on initial value type
152
+ if ( asObject ) {
153
+ bindAsObject ( vm , key , source , cancelCallback )
154
+ } else {
155
+ bindAsArray ( vm , key , source , cancelCallback )
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Bind a firebase data source to a key on a vm as an Array.
161
+ *
162
+ * @param {Vue } vm
163
+ * @param {string } key
164
+ * @param {object } source
165
+ * @param {function|null } cancelCallback
166
+ */
167
+ function bindAsArray ( vm , key , source , cancelCallback ) {
168
+ var array = [ ]
169
+ Vue . util . defineReactive ( vm , key , array )
170
+
171
+ var onAdd = source . on ( 'child_added' , function ( snapshot , prevKey ) {
172
+ var index = prevKey ? indexForKey ( array , prevKey ) + 1 : 0
173
+ array . splice ( index , 0 , createRecord ( snapshot ) )
174
+ } , cancelCallback )
175
+
176
+ var onRemove = source . on ( 'child_removed' , function ( snapshot ) {
177
+ var index = indexForKey ( array , _getKey ( snapshot ) )
178
+ array . splice ( index , 1 )
179
+ } , cancelCallback )
180
+
181
+ var onChange = source . on ( 'child_changed' , function ( snapshot ) {
182
+ var index = indexForKey ( array , _getKey ( snapshot ) )
183
+ array . splice ( index , 1 , createRecord ( snapshot ) )
184
+ } , cancelCallback )
185
+
186
+ var onMove = source . on ( 'child_moved' , function ( snapshot , prevKey ) {
187
+ var index = indexForKey ( array , _getKey ( snapshot ) )
188
+ var record = array . splice ( index , 1 ) [ 0 ]
189
+ var newIndex = prevKey ? indexForKey ( array , prevKey ) + 1 : 0
190
+ array . splice ( newIndex , 0 , record )
191
+ } , cancelCallback )
192
+
193
+ vm . _firebaseListeners [ key ] = {
194
+ child_added : onAdd ,
195
+ child_removed : onRemove ,
196
+ child_changed : onChange ,
197
+ child_moved : onMove
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Bind a firebase data source to a key on a vm as an Object.
203
+ *
204
+ * @param {Vue } vm
205
+ * @param {string } key
206
+ * @param {Object } source
207
+ * @param {function|null } cancelCallback
208
+ */
209
+ function bindAsObject ( vm , key , source , cancelCallback ) {
210
+ Vue . util . defineReactive ( vm , key , { } )
211
+ var cb = source . on ( 'value' , function ( snapshot ) {
212
+ vm [ key ] = createRecord ( snapshot )
213
+ } , cancelCallback )
214
+ vm . _firebaseListeners [ key ] = { value : cb }
215
+ }
216
+
217
+ /**
218
+ * Unbind a firebase-bound key from a vm.
219
+ *
220
+ * @param {Vue } vm
221
+ * @param {string } key
222
+ */
223
+ function unbind ( vm , key ) {
224
+ var source = vm . _firebaseSources && vm . _firebaseSources [ key ]
225
+ if ( ! source ) {
226
+ throw new Error (
227
+ 'VueFire: unbind failed: "' + key + '" is not bound to ' +
228
+ 'a Firebase reference.'
229
+ )
230
+ }
231
+ var listeners = vm . _firebaseListeners [ key ]
232
+ for ( var event in listeners ) {
233
+ source . off ( event , listeners [ event ] )
234
+ }
235
+ vm [ key ] = null
236
+ vm . $firebaseRefs [ key ] = null
237
+ vm . _firebaseSources [ key ] = null
238
+ vm . _firebaseListeners [ key ] = null
239
+ }
240
+
241
+ /**
242
+ * Ensure the related bookkeeping variables on an instance.
243
+ *
244
+ * @param {Vue } vm
245
+ */
246
+ function ensureRefs ( vm ) {
247
+ if ( ! vm . $firebaseRefs ) {
248
+ vm . $firebaseRefs = Object . create ( null )
249
+ vm . _firebaseSources = Object . create ( null )
250
+ vm . _firebaseListeners = Object . create ( null )
251
+ }
252
+ }
253
+
254
+ var init = function ( ) {
255
+ var bindings = this . $options . firebase
256
+ if ( ! bindings ) return
257
+ if ( typeof bindings === 'function' ) bindings = bindings . call ( this )
258
+ ensureRefs ( this )
259
+ for ( var key in bindings ) {
260
+ bind ( this , key , bindings [ key ] )
261
+ }
262
+ }
263
+
264
+ var VueFireMixin = {
265
+ init : init , // 1.x
266
+ beforeCreate : init , // 2.x
267
+ beforeDestroy : function ( ) {
268
+ if ( ! this . $firebaseRefs ) return
269
+ for ( var key in this . $firebaseRefs ) {
270
+ if ( this . $firebaseRefs [ key ] ) {
271
+ this . $unbind ( key )
272
+ }
273
+ }
274
+ this . $firebaseRefs = null
275
+ this . _firebaseSources = null
276
+ this . _firebaseListeners = null
277
+ }
278
+ }
279
+
280
+ /**
281
+ * Install function passed to Vue.use() in manual installation.
282
+ *
283
+ * @param {function } _Vue
284
+ */
285
+ function install ( _Vue ) {
286
+ Vue = _Vue
287
+ Vue . mixin ( VueFireMixin )
288
+
289
+ // use object-based merge strategy
290
+ var mergeStrats = Vue . config . optionMergeStrategies
291
+ mergeStrats . firebase = mergeStrats . methods
292
+
293
+ // extend instance methods
294
+ Vue . prototype . $bindAsObject = function ( key , source , cancelCallback ) {
295
+ ensureRefs ( this )
296
+ bind ( this , key , {
297
+ source : source ,
298
+ asObject : true ,
299
+ cancelCallback : cancelCallback
300
+ } )
301
+ }
302
+
303
+ Vue . prototype . $bindAsArray = function ( key , source , cancelCallback ) {
304
+ ensureRefs ( this )
305
+ bind ( this , key , {
306
+ source : source ,
307
+ cancelCallback : cancelCallback
308
+ } )
309
+ }
310
+
311
+ Vue . prototype . $unbind = function ( key ) {
312
+ unbind ( this , key )
313
+ }
314
+ }
315
+
316
+ // auto install
317
+ /* istanbul ignore if */
318
+ if ( typeof window !== 'undefined' && window . Vue ) {
319
+ install ( window . Vue )
320
+ }
321
+
322
+ module . exports = install
323
+
324
+
325
+ /***/ }
326
+ /******/ ] )
327
+ } ) ;
328
+ ;
0 commit comments