Skip to content

Instantly share code, notes, and snippets.

@arthurevans
Last active December 1, 2016 21:04
Show Gist options
  • Save arthurevans/0305937c642614422fc9f0b4a33efb1f to your computer and use it in GitHub Desktop.
Save arthurevans/0305937c642614422fc9f0b4a33efb1f to your computer and use it in GitHub Desktop.
Very basic polymerfire integration
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/polymerfire/firebase-app.html">
<link rel="import" href="../bower_components/polymerfire/firebase-auth.html">
<link rel="import" href="../bower_components/polymerfire/firebase-query.html">
<link rel="import" href="../bower_components/paper-button/paper-button.html">
<link rel="import" href="../bower_components/paper-input/paper-input.html">
<link rel="import" href="shared-styles.html">
<dom-module id="my-view1">
<template>
<style include="shared-styles">
:host {
display: block;
padding: 10px;
}
</style>
<firebase-app auth-domain="AUTH_DOMAIN.firebaseapp.com"
database-url="https://DB-DOMAIN.firebaseio.com/"
api-key="API_KEY">
</firebase-app>
<firebase-auth id="auth" user="{{user}}" provider="google" on-error="handleError">
</firebase-auth>
<firebase-query
id="query"
path="users/{{uid}}/messages"
data="{{data}}">
</firebase-query>
<template is="dom-repeat" items="{{data}}">
<div>[[item.text]]</div>
</template>
<paper-input label="New item" value="{{newText}}"></paper-input>
<paper-button on-tap="_addNew">Submit</paper-button>
</template>
<script>
Polymer({
is: 'my-view1',
properties: {
uid:
{
type: String,
computed: '_uidFor(user.*)'
},
data: {
type: Array,
value: function() {
return []
}
}
},
observers: [
'_dataChanged(data.*)'
],
ready: function() {
this.$.auth.signInWithPopup()
.then(function(response) { console.log('auth succeeded'); })
.catch(function(error) { console.log('auth failed'); console.log(error); });
},
handleError: function() {
console.log('auth error');
},
_uidFor(userCR) {
if (userCR && userCR.base) {
return userCR.base.uid;
}
},
_dataChanged: function (cR) {
console.log('data changed.');
console.log(cR.path);
console.log(cR);
console.log(cR.base);
// do something when the query returns values
},
_addNew: function() {
// DB location reference, instance of firebase.database.Reference
var ref = this.$.query.ref;
ref.push({text: this.newText})
.then(function(ref) { console.log('push succeeded'); })
.catch(function(error) { console.log('push failed', error); });
this.newText = "";
}
});
</script>
</dom-module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment