polymerfire icon indicating copy to clipboard operation
polymerfire copied to clipboard

polymer-document observer only triggers on item delete

Open krisdev-pl opened this issue 9 years ago • 7 comments

Description

I have firebase-document and

<firebase-document 
    path="/devices/userName" 
    data="{{data}}">
</firebase-document>


static get properties() {
    return {
        data: {
            type: Array,
            value: [],
            observer: 'dataChanged'
        }
    }
}
dataChanged(newData, oldData) {
    console.log("NewData: " + newData);
    this.data = newData;
}

Console log shows only when I delete items from my database, in every other case nothing happens until I refresh page and load data again.

Expected outcome

dataChanged is triggered after adding and editing data in database

krisdev-pl avatar Apr 25 '17 11:04 krisdev-pl

I'm using polymer 2.0-preview

krisdev-pl avatar Apr 25 '17 11:04 krisdev-pl

I'm having the same problem. I'm using polymer#2.0-preview and also polymerfire#2.0-preview.

New data is not received after the initial load. :-(

diegocerdan avatar May 08 '17 02:05 diegocerdan

This is my bower.json and a broken test case.

{
  "name": "test-fire",
  "main": "index.html",
  "dependencies": {
    "polymer": "2.0-preview",
    "polymerfire": "2.0-preview",
    "firebase": "3.9"
  },
  "devDependencies": {
    "web-component-tester": "^6.0.0-prerelease.5",
    "webcomponentsjs": "webcomponents/webcomponentsjs#^1.0.0-rc.7"
  },
  "resolutions": {
    "polymer": "^2.0.0-rc.3",
    "webcomponentsjs": "^1.0.0-rc.7",
    "polymerfire": "2.0-preview",
    "app-storage": "2.0-preview"
  }
}
<link rel="import" href="../../bower_components/polymer/polymer-element.html">

<link rel="import" href="../../bower_components/polymerfire/firebase-app.html">
<link rel="import" href="../../bower_components/polymerfire/firebase-document.html">

<dom-module id="test-fire-app">
  <template>
    <style>
      :host {
        display: block;
      }
    </style>

    <firebase-app
      auth-domain="santa-cruz-social.firebaseapp.com"
      database-url="https://santa-cruz-social.firebaseio.com"
      api-key="AIzaSyBMmk6wibiWCIOqlR7xJTMq3lcSAB2OlOo"
      storage-bucket="santa-cruz-social.appspot.com"
      messaging-sender-id="911046983483">
    </firebase-app>

    <firebase-document
      path="/tweets"
      data="{{tweets_data}}">
    </firebase-document>

    {{tweets_data}}

  </template>

  <script>
    class TestFireApp extends Polymer.Element {
      static get is() { return 'test-fire-app'; }
      static get properties() {
        return {
          tweets_data : {
            type: Array,
            value: function() {return [];},
            observer: '_tweetsDataChanged',
            notify: true
          }
        };
      }

      _tweetsDataChanged(newValue, oldValue) {
        console.log(newValue);
      }
    }

    window.customElements.define(TestFireApp.is, TestFireApp);
  </script>
</dom-module>

diegocerdan avatar May 08 '17 15:05 diegocerdan

@diegocerdan That is weird. I am getting the object in the console.log... You might need firebase-query if you want the tweets data

<firebase-query
      path="/tweets"
      data="{{tweets_data}}">
    </firebase-query>

    <template is="dom-repeat" items="[[tweets_data]]">
      [[item.text]]<br/><br/>
    </template>

image

tjmonsi avatar May 09 '17 08:05 tjmonsi

@krisdev-pl I don't think you need to do this.data = newData because the lifecycle of an observer is this

if "data" is updated via call from firebase-document, it calls "dataChanged" function and passes the new value of "data" to it

Now I see that you that data is an array. firebase-document will return either a value or an object. not an array. If you need an array structure, try to use firebase-query :)

tjmonsi avatar May 09 '17 08:05 tjmonsi

Thanks to @cdata at the slack channel I was able to solve it. He pointed out that change notifications for firebase-document happen at property level and you would need to replace the whole thing to get notified. And that can you can work around the issue by observing changes to sub-paths like ['_dataChanged(data.*)']. Anyway I ended using firebase-query instead and that works fine.

Maybe there should be a clear example on the documentation so people listen to changes properly.

diegocerdan avatar May 10 '17 20:05 diegocerdan