diff --git a/shared/base/view.js b/shared/base/view.js index c6d46ed0..88009f9b 100644 --- a/shared/base/view.js +++ b/shared/base/view.js @@ -231,7 +231,7 @@ module.exports = BaseView = Backbone.View.extend({ if (this.options.fetch_params) { if (!_.isObject(this.options.fetch_params)) { - throw new Error('fetch_params must be an object for lazy loaded views') + throw new Error('fetch_params must be an object for lazy loaded views'); } params = this.options.fetch_params; @@ -241,7 +241,7 @@ module.exports = BaseView = Backbone.View.extend({ if (this.options.fetch_options) { if (!_.isObject(this.options.fetch_options)) { - throw new Error('fetch_options must be an object for lazy loaded views') + throw new Error('fetch_options must be an object for lazy loaded views'); } fetchOptions = this.options.fetch_options; @@ -267,6 +267,15 @@ module.exports = BaseView = Backbone.View.extend({ }; } + // Allow ability to just pass the full "spec" to a lazy loaded view + if (this.options.fetch_spec) { + if (!_.isObject(this.options.fetch_spec)) { + throw new Error('fetch_spec must be an object for lazy loaded views'); + } + + fetchSpec = this.options.fetch_spec; + } + this.setLoading(true); this._preRender(); diff --git a/test/shared/base/view.test.js b/test/shared/base/view.test.js index 89d24a8b..99ed47e2 100644 --- a/test/shared/base/view.test.js +++ b/test/shared/base/view.test.js @@ -239,36 +239,61 @@ describe('BaseView', function() { }); describe('fetchLazy', function () { - var fetchParams = { model: 'Foo' }, - fetchOptions = { readFromCache: false }, - modelName = 'MyModel', - MyView, - view, - fetchSpec; - beforeEach(function () { - fetchSpec = { - model: { - model: modelName, - params: fetchParams - } + this.app = { + fetch: sinon.spy(), + modelUtils: modelUtils }; - MyView = BaseView.extend({ name: 'A View Name' }); - myView = new MyView({ - 'app': { fetch: sinon.stub() }, - 'model_name': modelName, - 'fetch_params': fetchParams, - 'fetch_options': fetchOptions + this.view = new this.MyTopView({ app: this.app }); + sinon.stub(this.view, 'setLoading'); + }); + + context('passed a fetch_spec', function () { + var fetchSpec; + + beforeEach(function () { + fetchSpec = { + model: { + model: 'Test', + params: { id: 1 } + } + }; + + this.view.options.fetch_spec = fetchSpec; }); - myView.setLoading = sinon.stub(); - myView._preRender = sinon.stub(); - myView.fetchLazy(); + it('overrides the fetchSpec and calls fetch with it.', function () { + this.view.fetchLazy(); + expect(this.app.fetch).to.have.been.calledWith(fetchSpec); + }); }); - it('passes the fetchSpec and fetch_options to the fetch call', function () { - expect(myView.app.fetch).to.have.been.calledWith(fetchSpec, fetchOptions, sinon.match.func); + context('passed fetch options', function () { + var fetchParams, fetchOptions; + + beforeEach(function () { + fetchParams = { id: 1 }; + fetchOptions = { + readFromCache: false, + writeToCache: true + }; + + this.view.options.model_name = 'MyModel'; + this.view.options.fetch_params = fetchParams; + this.view.options.fetch_options = fetchOptions; + }) + + it('invokes app.fetch with the fetchOptions', function () { + this.view.fetchLazy(); + + expect(this.app.fetch).to.have.been.calledWith({ + model: { + model: 'MyModel', + params: fetchParams + } + }, fetchOptions); + }); }); }); @@ -643,7 +668,7 @@ describe('BaseView', function() { topView.childViews.push(bottomView); childViews = topView.getChildViewsByName('my_bottom_view'); childViews.should.have.length(1); - bottomView.remove() + bottomView.remove(); childViews = topView.getChildViewsByName('my_bottom_view'); childViews.should.be.empty; }); @@ -656,49 +681,40 @@ describe('BaseView', function() { topView.$el = $('