Skip to content

Commit 8909faa

Browse files
committed
Merge pull request #447 from jmerrifield/dependency-injection
Dependency injection
2 parents d41f3a6 + f932400 commit 8909faa

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

shared/app.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ var Backbone = require('backbone'),
77
Fetcher = require('./fetcher'),
88
ModelUtils = require('./modelUtils'),
99
isServer = (typeof window === 'undefined'),
10-
ClientRouter;
10+
defaultRouterModule = 'app/router';
1111

1212
if (!isServer) {
13-
ClientRouter = require('app/router');
1413
Backbone.$ = window.$ || require('jquery');
1514
}
1615

@@ -56,8 +55,12 @@ module.exports = Backbone.Model.extend({
5655
* We can't use `this.get('templateAdapter')` here because `Backbone.Model`'s
5756
* constructor has not yet been called.
5857
*/
59-
var templateAdapterModule = attributes.templateAdapter || this.defaults.templateAdapter;
60-
this.templateAdapter = require(templateAdapterModule)({entryPath: entryPath});
58+
if (this.options.templateAdapterInstance) {
59+
this.templateAdapter = options.templateAdapterInstance;
60+
} else {
61+
var templateAdapterModule = attributes.templateAdapter || this.defaults.templateAdapter;
62+
this.templateAdapter = require(templateAdapterModule)({entryPath: entryPath});
63+
}
6164

6265
/**
6366
* Instantiate the `Fetcher`, which is used on client and server.
@@ -70,6 +73,8 @@ module.exports = Backbone.Model.extend({
7073
* Initialize the `ClientRouter` on the client-side.
7174
*/
7275
if (!isServer) {
76+
var ClientRouter = this.options.ClientRouter || require(defaultRouterModule);
77+
7378
new ClientRouter({
7479
app: this,
7580
entryPath: entryPath,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function (options) {
2+
return {
3+
name: 'Test template adapter',
4+
suppliedOptions: options
5+
};
6+
}

test/shared/app.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,41 @@ describe('BaseApp', function() {
2626
new MyApp();
2727
});
2828
});
29+
30+
describe('constructor', function() {
31+
context('with a custom templateAdapter module name', function() {
32+
beforeEach(function () {
33+
this.attributes = {templateAdapter: '../test/fixtures/app/template_adapter'};
34+
});
35+
36+
it('creates the templateAdapter we specify', function() {
37+
var app = new App(this.attributes);
38+
39+
expect(app.templateAdapter).to.have.property('name', 'Test template adapter');
40+
});
41+
42+
it('supplies the entryPath to the template adapter', function() {
43+
var app = new App(this.attributes, {entryPath: 'myEntryPath'});
44+
45+
expect(app.templateAdapter).to.have.deep.property('suppliedOptions.entryPath', 'myEntryPath');
46+
});
47+
});
48+
49+
context('with a concrete templateAdapterInstance', function() {
50+
it('uses the supplied templateAdapterInstance', function() {
51+
var myTemplateAdapter = {};
52+
var app = new App(null, {templateAdapterInstance: myTemplateAdapter});
53+
54+
expect(app.templateAdapter).to.equal(myTemplateAdapter);
55+
});
56+
57+
it('does not try to require a template adapter by name', function () {
58+
new App({
59+
templateAdapter: 'non existent module name - should throw'
60+
}, {
61+
templateAdapterInstance: {}
62+
});
63+
});
64+
});
65+
});
2966
});

0 commit comments

Comments
 (0)