Skip to content

Instantly share code, notes, and snippets.

@justinribeiro
Created July 30, 2015 00:16
Show Gist options
  • Save justinribeiro/3c7bc9f6a703080d47d8 to your computer and use it in GitHub Desktop.
Save justinribeiro/3c7bc9f6a703080d47d8 to your computer and use it in GitHub Desktop.
Access a specific record in a firebase-collection based on array bind in Polymer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Firebase + Polymer + Array Access</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/firebase-element/firebase-collection.html">
<link rel="import" href="person-by-index.html">
<style type="text/css">
body {
font-family: Roboto, Helvetica, sans-serif;
}
</style>
</head>
<body>
<template is="dom-bind">
<firebase-collection
location="https://polymer-ele-testing.firebaseio.com/people"
data="{{people}}" log></firebase-collection>
<person-by-index records="[[people]]" index="2"></person-by-index>
</template>
</body>
</html>
<link rel="import" href="bower_components/polymer/polymer.html">
<dom-module id="person-by-index">
<style>
:host {
display: block;
padding: 1em;
}
input {
width: 50%;
}
</style>
<template>
<div>
Index <span>[[index]]</span>:
<span>[[arrayItem(records.*, 'person')]]</span>
</div>
</template>
<script>
(function() {
Polymer({
is: 'person-by-index',
properties: {
records: Array,
index: Number
},
// respin from docs:
// https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#array-binding
arrayItem: function(change, path) {
return this.get(path, change.base[this.index]);
}
});
})();
</script>
</dom-module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment