Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 6497522

Browse files
committed
Merge pull request rendrjs#372 from saponifi3d/bootstrapData-recursive
Modify getBootstrappedData to check properties for more models and collections fixes rendrjs#263
2 parents 4dd5051 + 60ef124 commit 6497522

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

server/viewEngine.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,26 @@ ViewEngine.prototype.getViewHtml = function getViewHtml(viewPath, locals, app) {
7070
};
7171

7272
ViewEngine.prototype.getBootstrappedData = function getBootstrappedData(locals, app) {
73-
var bootstrappedData = {};
73+
var bootstrappedData = {},
74+
scope = this;
7475

7576
_.each(locals, function(modelOrCollection, name) {
7677
if (app.modelUtils.isModel(modelOrCollection) || app.modelUtils.isCollection(modelOrCollection)) {
7778
bootstrappedData[name] = {
7879
summary: app.fetcher.summarize(modelOrCollection),
7980
data: modelOrCollection.toJSON()
8081
};
82+
83+
if (app.modelUtils.isModel(modelOrCollection)) {
84+
_.each(modelOrCollection.attributes, function (value, key) {
85+
if (app.modelUtils.isModel(value) || app.modelUtils.isCollection(value)) {
86+
var tempObject = {};
87+
tempObject[key] = value;
88+
89+
_.defaults(bootstrappedData, scope.getBootstrappedData(tempObject, app));
90+
}
91+
})
92+
}
8193
}
8294
});
8395
return bootstrappedData;

test/server/viewEngine.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,55 @@ describe('ViewEngine', function() {
107107

108108
data.should.deep.equal({});
109109
});
110+
111+
it('should create a flat bootstrap object if a model has a nested model', function () {
112+
var bar = new Model({ id: 123 }),
113+
locals = {
114+
foo: new Model({ id: 321, bar: bar }, { app: app })
115+
},
116+
expectedData = {
117+
foo: {
118+
data: { bar: bar, id: 321 },
119+
summary: { model: 'model', id: 321 }
120+
},
121+
bar: {
122+
data: { id: 123 },
123+
summary: { model: 'model', id: 123 }
124+
}
125+
},
126+
data;
127+
128+
data = viewEngine.getBootstrappedData(locals, app);
129+
data.should.deep.equal(expectedData);
130+
});
131+
132+
it('should create a flat bootstrap object if a model has a nested collection', function () {
133+
var foo = new Model({ id: 321, foo: 'foo' }, { app: app }),
134+
baz = new Collection([foo], { app: app }),
135+
bar = new Model({ id: 123, foo: 'bar', items: baz }, { app: app }),
136+
locals = {
137+
foo: foo,
138+
bar: bar
139+
},
140+
expectedData = {
141+
foo: {
142+
data: { foo: 'foo', id: 321 },
143+
summary: { model: 'model', id: 321 }
144+
},
145+
bar: {
146+
data: { foo: 'bar', id: 123, items: baz },
147+
summary: { model: 'model', id: 123 }
148+
},
149+
items: {
150+
data: [ { foo: 'foo', id: 321 } ],
151+
summary: { collection: 'collection', ids: [ 321 ], meta: {}, params: {} }
152+
}
153+
},
154+
data;
155+
156+
data = viewEngine.getBootstrappedData(locals, app);
157+
data.should.deep.equal(expectedData);
158+
});
159+
110160
});
111161
});

0 commit comments

Comments
 (0)