Skip to content

Commit

Permalink
Merge pull request #412 from saponifi3d/template-data-with-options
Browse files Browse the repository at this point in the history
changed getTemplateData to also include the view options in the template
  • Loading branch information
saponifi3d committed Nov 24, 2014
2 parents 4152853 + aa1ff59 commit 7939fc9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
12 changes: 8 additions & 4 deletions shared/base/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,21 @@ module.exports = BaseView = Backbone.View.extend({
* Try to return proper data if model or collection is available.
*/
getTemplateData: function() {
var retVal, parsedOptions;

if (this.model) {
return this.model.toJSON();
retVal = this.model.toJSON();
} else if (this.collection) {
return {
retVal = {
models: this.collection.toJSON(),
meta: this.collection.meta,
params: this.collection.params
};
} else {
return _.clone(this.options);
}

// Remove options that are duplicates in the templates
parsedOptions = _.omit(this.options, ['model', 'collection', 'app']);
return _.extend({}, retVal, parsedOptions);
},

/**
Expand Down
91 changes: 91 additions & 0 deletions test/client/base_view.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,95 @@ describe('Base/View', function () {
});
});
});

describe('getTemplateData', function () {
beforeEach(function() {
this.subject.options = { test: 'test' };
});

it('should just return the options', function () {
expect(this.subject.getTemplateData()).to.deep.equal(this.subject.options);
});

it('should make a copy of the options', function () {
expect(this.subject.getTemplateData()).to.not.equal(this.subject.options);
});

it('should remove app from the templateData', function() {
this.subject.options.app = this.app;
expect(this.subject.getTemplateData()).to.deep.equal({
test: 'test'
});
});

context('there is a model', function () {
beforeEach(function () {
this.subject.model = new Model({
myOption: 'test'
});
});

it('should apply the options to the model data', function () {
expect(this.subject.getTemplateData()).to.deep.equal({
myOption: 'test',
test: 'test'
});
});

it('should override the model attribute if it is the same as the option on the view', function() {
this.subject.options.myOption = 'helloWorld';
expect(this.subject.getTemplateData()).to.deep.equal({
myOption: 'helloWorld',
test: 'test'
});
});

it('should remove the model from the options', function() {
this.subject.options.model = this.subject.model;

expect(this.subject.getTemplateData()).to.deep.equal({
myOption: 'test',
test: 'test'
});
});
});

context('there is a collection', function () {
var collectionModel = {
id: 1,
myOption: 'test'
};

var collectionMeta = { page: 1 },
collectionParams = { locale: 'en' };

beforeEach(function () {
this.subject.collection = new Collection([collectionModel]);
this.subject.collection.meta = collectionMeta;
this.subject.collection.params = collectionParams;
});

it('should return the models, collection meta information, and params with the options', function () {
expect(this.subject.getTemplateData()).to.deep.equal({
models: [collectionModel],
meta: collectionMeta,
params: collectionParams,
test: 'test'
});
});

it('should remove "collection" from the options', function() {
this.subject.options.collection = this.subject.collection;

expect(this.subject.getTemplateData()).to.deep.equal({
models: [collectionModel],
meta: collectionMeta,
params: collectionParams,
test: 'test'
});
});
});

});

});

0 comments on commit 7939fc9

Please sign in to comment.