From d0a4870d98e9861c0e89192174d3c5c928e2c4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natan=20S=C4=85gol?= Date: Wed, 25 Oct 2017 16:57:03 +0000 Subject: [PATCH 1/2] Fix an issue with initail binding in FirestoreMixin. This change fixes an issue which occurs when observed/bound properties are defined prior to a call of `connectedCallback`. Since _firestoreUpdateBinding method handles null and undefined values correctly, the `if` statement can be safely removed. --- firebase-firestore-mixin.html | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/firebase-firestore-mixin.html b/firebase-firestore-mixin.html index b4e9a19..423aaac 100644 --- a/firebase-firestore-mixin.html +++ b/firebase-firestore-mixin.html @@ -162,9 +162,7 @@ if (args.length) { args = ',' + args; } this._createMethodObserver(`_firestoreUpdateBinding('${type}','${name}'${args})`); - if (!config.props.length && !config.observes.length) { - this._firestoreUpdateBinding(type,name); - } + this._firestoreUpdateBinding(type,name); } _firestoreUpdateBinding(type, name) { From 5982940df56c120ed380930f3b2836005215baf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natan=20S=C4=85gol?= Date: Thu, 26 Oct 2017 16:55:06 +0000 Subject: [PATCH 2/2] Add query property to _firestoreProps in FirestoreMixin. --- firebase-firestore-mixin.html | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/firebase-firestore-mixin.html b/firebase-firestore-mixin.html index ac0258c..01ba8fe 100644 --- a/firebase-firestore-mixin.html +++ b/firebase-firestore-mixin.html @@ -141,38 +141,42 @@ connectedCallback() { const props = collect(this.constructor, 'properties'); for (let name in props) { - if (props[name].doc) { - this._firestoreBind('doc', props[name].doc, name, props[name].live, props[name].observes); - } else if (props[name].collection) { - this._firestoreBind('collection', props[name].collection, name, props[name].live, props[name].observes); + const options = props[name]; + if (options.doc || options.collection) { + this._firestoreBind(name, options); } } super.connectedCallback(); } - _firestoreBind(type, path, name, live = false, observes = []) { - const config = parsePath(path); - config.observes = observes; - config.live = live; + _firestoreBind(name, options) { + const defaults = { + live: false, + observes: [], + } + const parsedPath = parsePath(options.doc || options.collection); + const config = Object.assign({}, defaults, options, parsedPath); + config.type = config.doc ? 'doc' : 'collection'; this._firestoreProps[name] = config; // Create a method observer that will be called every time a templatized or observed property changes let args = config.props.concat(config.observes).join(','); if (args.length) { args = ',' + args; } - this._createMethodObserver(`_firestoreUpdateBinding('${type}','${name}'${args})`); + this._createMethodObserver(`_firestoreUpdateBinding('${name}'${args})`); if (!config.props.length && !config.observes.length) { - this._firestoreUpdateBinding(type,name); + this._firestoreUpdateBinding(name); } } - _firestoreUpdateBinding(type, name) { + _firestoreUpdateBinding(name, ...args) { this._firestoreUnlisten(name); const config = this._firestoreProps[name]; - const propArgs = Array.prototype.slice.call(arguments, 2, config.props.length + 2).filter(arg => arg); - const observesArgs = Array.prototype.slice.call(arguments, config.props.length + 2).filter(arg => arg); + const isDefined = (x) => x !== undefined; + const propArgs = args.slice(0, config.props.length).filter(isDefined); + const observesArgs = args.slice(config.props.length).filter(isDefined); if (propArgs.length < config.props.length || observesArgs.length < config.observes.length) { this[name] = null; @@ -183,11 +187,11 @@ const collPath = stitch(config.literals, propArgs); const assigner = snap => { - this[name] = TRANSFORMS[type](snap); + this[name] = TRANSFORMS[config.type](snap); this[name + 'Ready'] = true; } - let ref = this.db[type](collPath); + let ref = this.db[config.type](collPath); this[name + 'Ref'] = ref; this[name + 'Ready'] = false;