How to use Polymerfire to fetch data from a Firebase

521 views Asked by At

Description

My goal is to use the polymerfire element to send a request to a Firebase endpoint to detect if there is data there and, if so, its content.

Please include in your answer a working demo if possible and extra points if you point to some good documentation as the current documentation is insufficient.

Expected outcome

I expect the demo page to look like:

CLICK ME

ornithischia

foo bar baz

And when the CLICK ME button is pressed, the following to appear in the console:

"You clicked me!"

"triceratops"

"{appeared: -68000000, height: 3, length: 8, order: "ornithischia", vanished: -66000000, weight: 11000}"

Actual outcome

The the demo page looks like:

CLICK ME

foo bar baz

And when the CLICK ME button is pressed, the following actually appears in the console:

"You clicked me!"

"triceratops"

"{}"

Live Demo

http://jsbin.com/fasovaxonu/1/edit?html,console,output
<!doctype html>
<head>
  <meta charset="utf-8">
  <!-- Source: https://github.com/Download/polymer-cdn -->
  <base href="https://cdn.rawgit.com/download/polymer-cdn/1.7.0.2/lib/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="polymerfire/polymerfire.html">
  <link rel="import" href="paper-button/paper-button.html">
</head>

<body>

<dom-module id="x-element">

<template>
  <style></style>
  
  <firebase-app name="my-app"
                auth-domain="dinosaur-facts.firebaseapp.com"
                database-url="https://dinosaur-facts.firebaseio.com/"
                >
  </firebase-app>
  
  <p>
    <paper-button on-tap="_onTap">Click Me</paper-button>
  </p>
  <!---->
    <firebase-query
      app-name="my-app"              
      path="https://dinosaur-facts.firebaseio.com/dinosaurs"
      data="{{dinosaurs}}"
      >
    </firebase-query>
    <firebase-document
      app-name="my-app"              
      path="https://dinosaur-facts.firebaseio.com/dinosaurs/triceratops"
      data="{{triceratops}}"
      >
    </firebase-document>
  
    [[triceratops.order]]
  
    <br /><br />
  
    <template is="dom-repeat" items="[[dinosaurs]]">
      [[item.order]]
    </template>
    <template is="dom-repeat" items="[[test]]">
      [[item]]
    </template>
  <!---->

</template>

<script>
  (function(){
    Polymer({
      is: "x-element",
      properties: {
        dinosaurs: Array,
        test: {
          value: function() {
            return ['foo', 'bar', 'baz'];
          }
        },
      },
      _onTap: function() {
        console.log('You clicked me!');
        console.log('triceratops', JSON.stringify(this.triceratops));
      }
    });
  })();
</script>
 
</dom-module>

<x-element></x-element>

</body>
1

There are 1 answers

0
toto11 On BEST ANSWER

You first need to include <firebase-app> to initialize firebase.

<firebase-app 
 database-url="dinos-89701.firebaseio.com"
 api-key="AIzaSyDLkCy3RNC5uFomEjVsLUehpzKFDrfAplU"
 auth-domain="dinos-89701.firebaseio.com">
</firebase-app>

Get your api-key here:

https://console.firebase.google.com/project/YOUR_PROJECT/settings/general/

Demo: http://jsbin.com/joxatuz/1/edit?html,console,output