FirebaseExtended / polymerfire

Polymer Web Components for Firebase
https://www.webcomponents.org/element/firebase/polymerfire
MIT License
459 stars 142 forks source link

Polymer 2.0-Preview, polymer-query returns duplicated rows. #170

Closed mwinters-stuff closed 7 years ago

mwinters-stuff commented 7 years ago

Description

When using the polymer 2.0 preview, The firebase-query element returns the data array with the data duplicated. Eg, if the queried data on the server has 7 items, the firebase-query has 14, each item is listed twice.

Expected outcome

You get the 7 items with no duplicates.

Actual outcome

No specific example, just query a path and check the results.

Whats happening.

The __queryChanged is called more than once for the query, which seems to add more than one "child_added" e.t.c. event handlers.

Solve

I solved this by changing query changed to remove handlers if they were previously registered. ` queryChanged: function(query, oldQuery) {

      if (oldQuery) {
        oldQuery.off('child_added', this.__onFirebaseChildAdded, this);
        oldQuery.off('child_removed', this.__onFirebaseChildRemoved, this);
        oldQuery.off('child_changed', this.__onFirebaseChildChanged, this);
        oldQuery.off('child_moved', this.__onFirebaseChildMoved, this);

        this.syncToMemory(function() {
          this.set('data', this.zeroValue);
        });
      }

      if (query) {
        if(this._onOnce){ // remove handlers before adding again.
          query.off('child_added', this.__onFirebaseChildAdded, this);
          query.off('child_removed', this.__onFirebaseChildRemoved, this);
          query.off('child_changed', this.__onFirebaseChildChanged, this);
          query.off('child_moved', this.__onFirebaseChildMoved, this);
        }

        this._onOnce = true;
        query.on('child_added', this.__onFirebaseChildAdded, this.__onError, this);
        query.on('child_removed', this.__onFirebaseChildRemoved, this.__onError, this);
        query.on('child_changed', this.__onFirebaseChildChanged, this.__onError, this);
        query.on('child_moved', this.__onFirebaseChildMoved, this.__onError, this);
      }
    },

`

BrandiATMuhkuh commented 7 years ago

same issue here.

-- EDIT -- I have changed my firebase-query.html code to the once @wintersandroid proposed and it works like a charm.

Borjagodoy commented 7 years ago

Same problem with the Polymer2.0 preview rc and this is a solution.

Can you fix this problem in a PR?

Good Job and thanks @wintersandroid

mkutny commented 7 years ago

Works like a charm.

Thanks @wintersandroid !

Oupsla commented 7 years ago

Thanks @wintersandroid !

Yeah need to fix this problem in a PR

mwinters-stuff commented 7 years ago

I will fork it now and submit a PR...

e111077 commented 7 years ago

merged into 2.0-preview