From 0a847f5c258ab18c98171b2d12d65346c1c6f91f Mon Sep 17 00:00:00 2001 From: Josh Callender Date: Fri, 14 Nov 2014 12:07:52 -0800 Subject: [PATCH 1/3] changed getTemplateData to also include the view options in the template. --- shared/base/view.js | 10 +++--- test/client/base_view.test.js | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/shared/base/view.js b/shared/base/view.js index 27e0b5ca..906b3856 100644 --- a/shared/base/view.js +++ b/shared/base/view.js @@ -106,17 +106,19 @@ module.exports = BaseView = Backbone.View.extend({ * Try to return proper data if model or collection is available. */ getTemplateData: function() { + var retVal; + 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); } + + return _.extend({}, retVal, this.options); }, /** diff --git a/test/client/base_view.test.js b/test/client/base_view.test.js index 2ba06bce..3324893e 100644 --- a/test/client/base_view.test.js +++ b/test/client/base_view.test.js @@ -190,4 +190,65 @@ 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); + }); + + 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' + }); + }); + }); + + 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' + }); + }); + }); + + }); + }); From 715fbf710d5e88617f93fef03a729b6be38c046a Mon Sep 17 00:00:00 2001 From: Josh Callender Date: Fri, 14 Nov 2014 12:18:25 -0800 Subject: [PATCH 2/3] added a quick test to make sure that the options obejct is being cloned correclty --- test/client/base_view.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/client/base_view.test.js b/test/client/base_view.test.js index 3324893e..f90ec179 100644 --- a/test/client/base_view.test.js +++ b/test/client/base_view.test.js @@ -200,6 +200,10 @@ describe('Base/View', 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); + }); + context('there is a model', function () { beforeEach(function () { this.subject.model = new Model({ @@ -232,7 +236,6 @@ describe('Base/View', function () { var collectionMeta = { page: 1 }, collectionParams = { locale: 'en' }; - beforeEach(function () { this.subject.collection = new Collection([collectionModel]); this.subject.collection.meta = collectionMeta; From aa1ff59b7211bdae534413abbe44583b8a548313 Mon Sep 17 00:00:00 2001 From: Josh Callender Date: Tue, 18 Nov 2014 11:35:25 -0800 Subject: [PATCH 3/3] remove extra variables that can be set in the options; model, collection, and app are all set in the templateAdapters prefixed with _'s. --- shared/base/view.js | 6 ++++-- test/client/base_view.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/shared/base/view.js b/shared/base/view.js index 906b3856..9dc18568 100644 --- a/shared/base/view.js +++ b/shared/base/view.js @@ -106,7 +106,7 @@ module.exports = BaseView = Backbone.View.extend({ * Try to return proper data if model or collection is available. */ getTemplateData: function() { - var retVal; + var retVal, parsedOptions; if (this.model) { retVal = this.model.toJSON(); @@ -118,7 +118,9 @@ module.exports = BaseView = Backbone.View.extend({ }; } - return _.extend({}, retVal, this.options); + // Remove options that are duplicates in the templates + parsedOptions = _.omit(this.options, ['model', 'collection', 'app']); + return _.extend({}, retVal, parsedOptions); }, /** diff --git a/test/client/base_view.test.js b/test/client/base_view.test.js index f90ec179..d745b4c5 100644 --- a/test/client/base_view.test.js +++ b/test/client/base_view.test.js @@ -204,6 +204,13 @@ describe('Base/View', 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({ @@ -225,6 +232,15 @@ describe('Base/View', function () { 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 () { @@ -250,6 +266,17 @@ describe('Base/View', function () { 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' + }); + }); }); });