Skip to content

Commit b049d89

Browse files
committed
[fixed] list children not being transformed
1 parent 1cc8f6e commit b049d89

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ var createRefForReals = (Firebase, path, routes, host) => {
9191
if (snapshotVal === null)
9292
return cb(null, null);
9393
var transformed = handler.transform ? handler.transform(snapshotVal) : snapshotVal;
94-
addIdForLists(snapshotVal, matchInfo.route, path);
94+
if (matchInfo.route.handler == 'list')
95+
transformed = transformListChildren(transformed, routes, path);
96+
addIdForDirectRefToListChild(transformed, matchInfo.route, path);
9597
addRelationships(transformed, matchInfo);
9698
addIndexes(transformed, matchInfo);
9799
cb(null, transformed);
@@ -118,7 +120,23 @@ var createRefForReals = (Firebase, path, routes, host) => {
118120
return { set:set, getValue, push, child, listen };
119121
};
120122

121-
var addIdForLists = (val, route, path) => {
123+
var transformListChildren = (children, routes, path) => {
124+
// should probably recurse here
125+
return children.map((child) => {
126+
Object.keys(child).forEach((key) => {
127+
if (key === '_id') // hmmm this might get out of hand
128+
return;
129+
var childPath = `${path}/${child._id}/${key}`;
130+
var matchInfo = router.match(childPath, routes);
131+
var handler = matchInfo.route.handler;
132+
if (handler.transform)
133+
child[key] = handler.transform(child[key]);
134+
});
135+
return child;
136+
});
137+
};
138+
139+
var addIdForDirectRefToListChild = (val, route, path) => {
122140
if (route.parent && route.parent.handler == 'list')
123141
val._id = path.split('/').reverse()[0];
124142
};

tests.js

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('schema', () => {
2828
schema.createRef('not-defined')
2929
}).toThrow(/not found/);
3030
});
31+
3132
});
3233

3334
describe('ref', () => {
@@ -43,6 +44,29 @@ describe('ref', () => {
4344
});
4445
});
4546

47+
it('transforms children', (done) => {
48+
var testType = {
49+
validate () {},
50+
transform (val) {
51+
return val + 1;
52+
}
53+
};
54+
var schema = Schema.create(Firebase, HOST, (child) => {
55+
child('test', list, (child) => {
56+
child(':id', hash, (child) => {
57+
child('n', testType);
58+
});
59+
});
60+
});
61+
var ref = schema.createRef('test');
62+
ref.push({ n: 10 }, () => {
63+
ref.getValue((err, val) => {
64+
expect(val).toEqual([{ n: 11, _id: val[0]._id }]);
65+
done();
66+
});
67+
});
68+
});
69+
4670
describe('getValue', () => {
4771
it('handles null values', (done) => {
4872
var schema = Schema.create(Firebase, HOST, (child) => {

0 commit comments

Comments
 (0)