Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the getBootstrappedData function to add each model of a collection to the modelStore #380

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions server/viewEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 25 additions & 14 deletions test/server/viewEngine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
});
Expand All @@ -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 }
}
Expand All @@ -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);
});
Expand Down