diff --git a/server/viewEngine.js b/server/viewEngine.js index f533901a..07b9737c 100644 --- a/server/viewEngine.js +++ b/server/viewEngine.js @@ -80,16 +80,17 @@ ViewEngine.prototype.getBootstrappedData = function getBootstrappedData(locals, data: modelOrCollection.toJSON() }; - if (app.modelUtils.isModel(modelOrCollection)) { - _.each(modelOrCollection.attributes, function (value, key) { - if (app.modelUtils.isModel(value) || app.modelUtils.isCollection(value)) { - var tempObject = {}; - tempObject[key] = value; - - _.defaults(bootstrappedData, scope.getBootstrappedData(tempObject, app)); - } - }) - } + + var list = (app.modelUtils.isModel(modelOrCollection)) ? modelOrCollection.attributes: modelOrCollection.models; + _.each(list, function (value, key) { + if (app.modelUtils.isModel(value) || app.modelUtils.isCollection(value)) { + var tempObject = {}, + key = app.modelUtils.modelName(value.constructor) + ':' + value.id + + tempObject[key] = value; + _.defaults(bootstrappedData, scope.getBootstrappedData(tempObject, app)); + } + }) } }); return bootstrappedData; diff --git a/test/server/viewEngine.test.js b/test/server/viewEngine.test.js index c44d3282..07b7b51f 100644 --- a/test/server/viewEngine.test.js +++ b/test/server/viewEngine.test.js @@ -82,7 +82,7 @@ describe('ViewEngine', function() { it('should create bootstrap data from models and collection', function () { var locals = { - foo: new Model({ id: 321, foo: 'bar' }, { app: app }), + foo: new Model({ id: 321, foo: 'bar' }, { app: app }), bar: new Collection([ new Model({ id: 123, foo: 'bar' }, { app: app} ) ], { app: app }) }, expectedData = { @@ -97,6 +97,11 @@ describe('ViewEngine', function() { }, data; + expectedData['model:' + locals.bar.models[0].id] = { + data: { foo: 'bar', id: 123 }, + summary: { model: 'model', id: 123 } + } + data = viewEngine.getBootstrappedData(locals, app); data.should.deep.equal(expectedData); }); @@ -118,7 +123,7 @@ describe('ViewEngine', function() { data: { bar: bar, id: 321 }, summary: { model: 'model', id: 321 } }, - bar: { + 'model:123': { data: { id: 123 }, summary: { model: 'model', id: 123 } } @@ -130,29 +135,35 @@ describe('ViewEngine', function() { }); it('should create a flat bootstrap object if a model has a nested collection', function () { - var foo = new Model({ id: 321, foo: 'foo' }, { app: app }), + var foo = new Model({ id: 321, foo: 'foo' }, { app: app }), baz = new Collection([foo], { app: app }), - bar = new Model({ id: 123, foo: 'bar', items: baz }, { app: app }), + bar = new Model({ id: 123, foo: 'bar', items: baz }, { app: app }), locals = { - foo: foo, bar: bar }, expectedData = { - foo: { - data: { foo: 'foo', id: 321 }, - summary: { model: 'model', id: 321 } - }, bar: { - data: { foo: 'bar', id: 123, items: baz }, - summary: { model: 'model', id: 123 } + data: { foo: 'bar', id: 123, items: baz }, + summary: { model: 'model', id: 123 } }, - items: { - data: [ { foo: 'foo', id: 321 } ], - summary: { collection: 'collection', ids: [ 321 ], meta: {}, params: {} } + 'model:321': { + data: { foo: 'foo', id: 321 }, + summary: { model: 'model', id: 321 } } }, data; + baz.id = 111; + expectedData['collection:' + baz.id] = { + data: [{ foo: 'foo', id: 321 }], + summary: { + collection: 'collection', + ids: [321], + meta: {}, + params: {} + } + }; + data = viewEngine.getBootstrappedData(locals, app); data.should.deep.equal(expectedData); });