Added $key to bound objects

This commit is contained in:
jwngr
2015-07-08 17:09:15 -07:00
parent 5710cf5801
commit f403f90188
2 changed files with 50 additions and 31 deletions

View File

@@ -84,6 +84,25 @@
}
}
/**
* Creates a new array record record given a key-value pair.
*
* @param {string} key The new record's key.
* @param {any} value The new record's value.
* @return {Object} The new record.
*/
function _createRecord(key, value) {
var record = {};
if (typeof value === 'object' && value !== null) {
record = value;
} else {
record.$value = value;
}
record.$key = key;
return record;
}
/******************************/
/* BIND AS OBJECT LISTENERS */
@@ -95,7 +114,11 @@
* @param {Firebase.DataSnapshot} snapshot A snapshot of the data being bound.
*/
function _objectValue(bindVar, snapshot) {
this.data[bindVar] = snapshot.val();
var key = snapshot.key();
var value = snapshot.val();
this.data[bindVar] = _createRecord(key, value);
this.setState(this.data);
}
@@ -103,25 +126,6 @@
/*****************************/
/* BIND AS ARRAY LISTENERS */
/*****************************/
/**
* Creates a new array record record given a key-value pair.
*
* @param {string} key The new record's key.
* @param {any} value The new record's value.
* @return {Object} The new record.
*/
function _createRecord(key, value) {
var record = {};
if (typeof value === 'object') {
record = value;
} else {
record.$value = value;
}
record.$key = key;
return record;
}
/**
* 'child_added' listener which adds a new record to the bound array.
*
@@ -291,6 +295,7 @@
*/
componentWillUnmount: function() {
for (var bindVar in this.firebaseRefs) {
/* istanbul ignore else */
if (this.firebaseRefs.hasOwnProperty(bindVar)) {
this.unbind(bindVar);
}
@@ -340,6 +345,7 @@
// Turn off all Firebase listeners
for (var event in this.firebaseListeners[bindVar]) {
/* istanbul ignore else */
if (this.firebaseListeners[bindVar].hasOwnProperty(event)) {
var offListener = this.firebaseListeners[bindVar][event];
this.firebaseRefs[bindVar].off(event, offListener);

View File

@@ -607,7 +607,7 @@ describe('ReactFire', function() {
mixins: [ReactFireMixin],
componentWillMount: function() {
this.bindAsObject(firebaseRef, 'items');
this.bindAsObject(firebaseRef.child('items'), 'items');
var obj = {
first: { index: 0 },
@@ -615,7 +615,8 @@ describe('ReactFire', function() {
third: { index: 2 }
};
firebaseRef.set(obj, function() {
firebaseRef.child('items').set(obj, function() {
obj.$key = 'items';
expect(this.state.items).to.deep.equal(obj);
done();
@@ -635,10 +636,13 @@ describe('ReactFire', function() {
mixins: [ReactFireMixin],
componentWillMount: function() {
this.bindAsObject(firebaseRef, 'items');
this.bindAsObject(firebaseRef.child('items'), 'items');
firebaseRef.set('foo', function() {
expect(this.state.items).to.deep.equal('foo');
firebaseRef.child('items').set('foo', function() {
expect(this.state.items).to.deep.equal({
$key: 'items',
$value: 'foo'
});
done();
}.bind(this));
@@ -652,15 +656,18 @@ describe('ReactFire', function() {
shallowRenderer.render(React.createElement(TestComponent));
});
it('binds as null for Firebase references with no data', function(done) {
it('binds to Firebase references with no data', function(done) {
var TestComponent = React.createClass({
mixins: [ReactFireMixin],
componentWillMount: function() {
this.bindAsObject(firebaseRef, 'items');
this.bindAsObject(firebaseRef.child('items'), 'items');
firebaseRef.set(null, function() {
expect(this.state.items).to.be.null;
firebaseRef.child('items').set(null, function() {
expect(this.state.items).to.deep.equal({
$key: 'items',
$value: null
});
done();
}.bind(this));
@@ -679,14 +686,15 @@ describe('ReactFire', function() {
mixins: [ReactFireMixin],
componentWillMount: function() {
this.bindAsObject(firebaseRef.limitToLast(2), 'items');
this.bindAsObject(firebaseRef.child('items').limitToLast(2), 'items');
firebaseRef.set({
firebaseRef.child('items').set({
first: { index: 0 },
second: { index: 1 },
third: { index: 2 }
}, function() {
expect(this.state.items).to.deep.equal({
$key: 'items',
second: { index: 1 },
third: { index: 2 }
});
@@ -729,7 +737,10 @@ describe('ReactFire', function() {
items0: items0,
items1: items1
}, function() {
items0.$key = 'items0';
expect(this.state.bindVar0).to.deep.equal(items0);
items1.$key = 'items1';
expect(this.state.bindVar1).to.deep.equal(items1);
done();
@@ -770,7 +781,9 @@ describe('ReactFire', function() {
items0: items0,
items1: items1
}, function() {
items0.$key = 'items0';
expect(this.state.bindVar0).to.deep.equal(items0);
expect(this.state.bindVar1).to.deep.equal([
{ $key: 'bar', foo: 'baz' },
{ $key: 'baz', $value: true },