Skip to content

Commit 9b0aa1b

Browse files
committed
Merge pull request #26 from GoogleWebComponents/fix-firebase-collection-child-changed
Fixes Firebase Collection child changed
2 parents ed97284 + 6012faf commit 9b0aa1b

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

firebase-collection.html

+15-1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ <h4>[[dinosaur.__firebaseKey__]]</h4>
268268
this.query.ref().child(key).remove();
269269
},
270270

271+
_applyLocalDataChanges: function(change) {
272+
var pathParts = change.path.split('.');
273+
var index;
274+
var value;
275+
276+
if (pathParts.length < 2 || pathParts[1] === 'splices') {
277+
return;
278+
}
279+
280+
index = parseInt(pathParts[1], 10);
281+
value = this.data[index];
282+
this.query.ref().child(value.__firebaseKey__).set(value);
283+
},
284+
271285
_computeQuery: function(location, limitToFirst, limitToLast, orderByMethodName, startAt, endAt, equalTo) {
272286
var query;
273287

@@ -431,7 +445,7 @@ <h4>[[dinosaur.__firebaseKey__]]</h4>
431445

432446
this._valueMap[oldValue.__firebaseKey__] = null;
433447
this._valueMap[value.__firebaseKey__] = value;
434-
this.splice('data' + this.data.indexOf(oldValue), 1, value);
448+
this.splice('data', this.data.indexOf(oldValue), 1, value);
435449
});
436450
},
437451

test/firebase-collection.html

+58
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
</firebase-collection>
4141
</template>
4242
</test-fixture>
43+
<test-fixture id="ChangingChildren">
44+
<template>
45+
<firebase-collection
46+
location="https://fb-element-demo.firebaseio.com/test/changing_children"
47+
order-by-child="foo"
48+
log>
49+
</firebase-collection>
50+
</template>
51+
</test-fixture>
4352

4453
<test-fixture id="SyncingCollections">
4554
<template>
@@ -148,6 +157,55 @@
148157
});
149158
});
150159

160+
suite('a child changes', function() {
161+
setup(function(done) {
162+
firebase = fixture('ChangingChildren');
163+
waitForEvent(firebase, 'firebase-value').then(function() {
164+
done();
165+
}).catch(function(e) {
166+
done(e)
167+
});
168+
});
169+
170+
test('updates the child key in place with the new value', function(done) {
171+
var childrenKeys = [];
172+
173+
waitForEvent(firebase, 'firebase-value').then(function() {
174+
var middleValue = firebase.getByKey(childrenKeys[1]);
175+
var changes;
176+
177+
expect(middleValue.foo).to.be.equal(1);
178+
expect(middleValue.bar).to.be.equal(1);
179+
180+
changes = waitForEvent(firebase, 'firebase-child-changed');
181+
182+
firebase.set('data.' + firebase.data.indexOf(middleValue) + '.bar', 2);
183+
184+
return changes;
185+
}).then(function() {
186+
var middleValue = firebase.getByKey(childrenKeys[1]);
187+
188+
expect(middleValue.foo).to.be.equal(1);
189+
expect(middleValue.bar).to.be.equal(2);
190+
191+
done();
192+
}).catch(function(e) {
193+
done(e);
194+
}).then(function() {
195+
childrenKeys.forEach(function(key) {
196+
firebase.removeByKey(key);
197+
});
198+
});
199+
200+
childrenKeys = [0, 1, 2].map(function(value, index) {
201+
return firebase.add({
202+
foo: value,
203+
bar: index
204+
}).key();
205+
});
206+
});
207+
});
208+
151209
suite('syncing collections', function() {
152210
var localFirebase;
153211
var remoteFirebase;

0 commit comments

Comments
 (0)