Skip to content

Commit

Permalink
FirebaseList mapKey
Browse files Browse the repository at this point in the history
  • Loading branch information
barbuza committed Feb 10, 2016
1 parent 0a2b4ab commit ccf9641
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "kebakaran",
"version": "0.1.13",
"version": "0.1.14",
"description": "high level utilities for firebase interaction",
"main": "index.js",
"scripts": {
"prepublish": "npm run lint && npm test && npm run build",
"prepublish": "git diff --cached --exit-code && git diff --exit-code && npm run lint && npm test && npm run build",
"build": "rm -rf lib && babel -d lib src",
"lint": "eslint src tests",
"test": "nyc -i babel-register -i babel-polyfill tape tests/*-tape.js | faucet",
Expand Down
11 changes: 7 additions & 4 deletions src/FirebaseList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import { FirebaseStruct } from './FirebaseStruct';

const NO_VALUE = Symbol();

const getKey = snapshot => snapshot.key();

export class FirebaseList extends EventEmitter {

keys = [];
items = {};
values = {};

constructor(ref, getFields, idField = 'id', instant = false) {
constructor(ref, getFields, idField = 'id', instant = false, mapKey = getKey) {
super();

this.ref = ref;
this.getFields = getFields;
this.idField = idField;
this.instant = instant;
this.hasInitialData = !instant;
this.mapKey = mapKey;

this.subscribe();
}
Expand All @@ -44,7 +47,7 @@ export class FirebaseList extends EventEmitter {
onValue(snapshot) {
const newKeys = [];
snapshot.forEach(itemSnapshot => {
newKeys.push(itemSnapshot.key());
newKeys.push(this.mapKey(itemSnapshot));
});
for (const key of newKeys) {
if (this.keys.indexOf(key) === -1) {
Expand All @@ -62,12 +65,12 @@ export class FirebaseList extends EventEmitter {
}

onChildAdded(c) {
const key = c.key();
const key = this.mapKey(c);
this.addChild(key);
}

onChildRemoved(c) {
const key = c.key();
const key = this.mapKey(c);
this.removeChild(key);
this.flush();
}
Expand Down

0 comments on commit ccf9641

Please sign in to comment.