Skip to content

Commit a15124f

Browse files
committed
added fastboot shoebox
1 parent a3048f2 commit a15124f

34 files changed

Lines changed: 420 additions & 48 deletions

Brocfile.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

app/services/fastboot.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Ember from "ember";
33

44
const { deprecate, computed, get } = Ember;
5-
const { alias, deprecatingAlias, readOnly } = computed;
5+
const { deprecatingAlias, readOnly } = computed;
66

77
const RequestObject = Ember.Object.extend({
88
init() {
@@ -18,18 +18,59 @@ const RequestObject = Ember.Object.extend({
1818
this.protocol = request.protocol;
1919
this._host = function() {
2020
return request.host();
21-
}
21+
};
2222
},
2323

2424
host: computed(function() {
2525
return this._host();
2626
})
2727
});
2828

29+
const Shoebox = Ember.Object.extend({
30+
put(key, value) {
31+
Ember.assert('shoebox.put is only invoked from the FastBoot rendered application', this.get('fastboot.isFastBoot'));
32+
Ember.assert('the provided key is a string', typeof key === 'string');
33+
34+
let fastbootInfo = this.get('fastboot._fastbootInfo');
35+
if (!fastbootInfo.shoebox) { fastbootInfo.shoebox = {}; }
36+
37+
fastbootInfo.shoebox[key] = value;
38+
},
39+
40+
retrieve(key) {
41+
if (this.get('fastboot.isFastBoot')) {
42+
let shoebox = this.get('fastboot._fastbootInfo.shoebox');
43+
if (!shoebox) { return; }
44+
45+
return shoebox[key];
46+
}
47+
48+
let shoeboxItem = this.get(key);
49+
if (shoeboxItem) { return shoeboxItem; }
50+
51+
let $el = Ember.$(`#shoebox-${key}`);
52+
if (!$el.length) { return; }
53+
let valueString = $el.text();
54+
if (!valueString) { return; }
55+
56+
shoeboxItem = JSON.parse(valueString);
57+
this.set(key, shoeboxItem);
58+
59+
return shoeboxItem;
60+
}
61+
});
62+
2963
export default Ember.Service.extend({
3064
cookies: deprecatingAlias('request.cookies', { id: 'fastboot.cookies-to-request', until: '0.9.9' }),
3165
headers: deprecatingAlias('request.headers', { id: 'fastboot.headers-to-request', until: '0.9.9' }),
3266

67+
init() {
68+
this._super(...arguments);
69+
70+
let shoebox = Shoebox.create({ fastboot: this });
71+
this.set('shoebox', shoebox);
72+
},
73+
3374
host: computed(function() {
3475
deprecate(
3576
'Usage of fastboot service\'s `host` property is deprecated. Please use `request.host` instead.',

bower.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
"ember": "canary",
66
"ember-data": "canary",
77
"ember-resolver": "~0.1.12",
8-
"loader.js": "ember-cli/loader.js#3.2.0",
9-
"ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3",
10-
"ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
11-
"ember-load-initializers": "ember-cli/ember-load-initializers#0.0.2",
8+
"ember-cli-shims": "0.1.0",
9+
"ember-cli-test-loader": "0.2.2",
1210
"ember-qunit": "0.2.8",
1311
"ember-qunit-notifications": "0.0.7",
1412
"qunit": "~1.17.1"

ember-cli-build.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*jshint node:true*/
2+
/* global require, module */
3+
var EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
4+
5+
module.exports = function(defaults) {
6+
var app = new EmberAddon(defaults, {
7+
// Add options here
8+
});
9+
10+
/*
11+
This build file specifes the options for the dummy test app of this
12+
addon, located in `/tests/dummy`
13+
This build file does *not* influence how the addon or the app using it
14+
behave. You most likely want to be modifying `./index.js` or app's build file
15+
*/
16+
17+
return app.toTree();
18+
};

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"start": "ember server",
1212
"build": "ember build",
13-
"test": "mocha",
13+
"test": "mocha && ember test",
1414
"test:precook": "node node_modules/ember-cli-addon-tests/scripts/precook-node-modules.js"
1515
},
1616
"engines": {
@@ -38,10 +38,13 @@
3838
"ember-cli-uglify": "^1.2.0",
3939
"ember-data": "1.13.8",
4040
"ember-export-application-global": "^1.0.3",
41+
"ember-load-initializers": "^0.5.1",
42+
"ember-resolver": "^2.0.3",
4143
"exists-sync": "0.0.3",
4244
"fs-extra": "^0.24.0",
4345
"fs-promise": "^0.3.1",
4446
"glob": "^7.0.0",
47+
"loader.js": "^4.0.1",
4548
"mocha": "^2.2.4",
4649
"request": "^2.55.0",
4750
"symlink-or-copy": "^1.0.1",
@@ -62,7 +65,9 @@
6265
"broccoli-merge-trees": "^1.1.1",
6366
"broccoli-plugin": "^1.2.1",
6467
"broccoli-stew": "^1.2.0",
65-
"fastboot-express-middleware": "1.0.0-beta.3",
68+
"fastboot-express-middleware": "1.0.0-beta.5",
69+
"ember-cli-babel": "^5.1.5",
70+
"ember-test-helpers": "^0.5.22",
6671
"express": "^4.8.5",
6772
"fastboot-filter-initializers": "0.0.2",
6873
"lodash.clonedeep": "^4.3.1",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Route.extend({
4+
fastboot: Ember.inject.service(),
5+
shoebox: Ember.computed.readOnly('fastboot.shoebox'),
6+
7+
model() {
8+
let fastboot = this.get('fastboot');
9+
let shoebox = this.get('shoebox');
10+
if (fastboot.get('isFastBoot')) {
11+
shoebox.put('key1', { foo: 'bar' });
12+
shoebox.put('key2', { zip: 'zap' });
13+
}
14+
}
15+
});

test/helpers/destroy-app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Ember from 'ember';
2+
3+
export default function destroyApp(application) {
4+
Ember.run(application, 'destroy');
5+
}

test/helpers/start-app.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Ember from 'ember';
2+
import Application from '../fixtures/fanny-pack/app';
3+
import config from '../../config/environment';
4+
5+
export default function startApp(attrs) {
6+
let application;
7+
8+
let attributes = Ember.merge({}, config.APP);
9+
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;
10+
11+
Ember.run(() => {
12+
application = Application.create(attributes);
13+
application.setupForTesting();
14+
application.injectTestHelpers();
15+
});
16+
17+
return application;
18+
}

test/shoebox-put-test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var expect = require('chai').expect;
2+
var RSVP = require('rsvp');
3+
var request = RSVP.denodeify(require('request'));
4+
5+
var AddonTestApp = require('ember-cli-addon-tests').AddonTestApp;
6+
7+
describe('shoebox - put', function() {
8+
this.timeout(300000);
9+
10+
var app;
11+
12+
before(function() {
13+
app = new AddonTestApp();
14+
15+
return app.create('shoebox')
16+
.then(function() {
17+
return app.startServer({
18+
command: 'fastboot'
19+
});
20+
});
21+
});
22+
23+
after(function() {
24+
return app.stopServer();
25+
});
26+
27+
it('put items into the shoebox', function() {
28+
return request('http://localhost:49741/')
29+
.then(function(response) {
30+
expect(response.statusCode).to.equal(200);
31+
expect(response.body).to.contain(
32+
'<script type="fastboot/shoebox" id="shoebox-key1">' +
33+
'{"foo":"bar"}' +
34+
'</script>'
35+
);
36+
expect(response.body).to.contain(
37+
'<script type="fastboot/shoebox" id="shoebox-key2">' +
38+
'{"zip":"zap"}' +
39+
'</script>'
40+
);
41+
});
42+
});
43+
});

tests/.jshintrc

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
{
22
"predef": [
3+
"document",
4+
"window",
5+
"location",
6+
"setTimeout",
7+
"$",
8+
"-Promise",
9+
"define",
310
"console",
4-
"it",
5-
"describe",
6-
"beforeEach",
7-
"afterEach",
8-
"before",
9-
"after",
10-
"-Promise"
11+
"visit",
12+
"exists",
13+
"fillIn",
14+
"click",
15+
"keyEvent",
16+
"triggerEvent",
17+
"find",
18+
"findWithAssert",
19+
"wait",
20+
"DS",
21+
"andThen",
22+
"currentURL",
23+
"currentPath",
24+
"currentRouteName"
1125
],
12-
"unused": true,
13-
"node": true,
26+
"node": false,
1427
"browser": false,
1528
"boss": true,
16-
"curly": false,
29+
"curly": true,
1730
"debug": false,
1831
"devel": false,
1932
"eqeqeq": true,
@@ -34,5 +47,6 @@
3447
"strict": false,
3548
"white": false,
3649
"eqnull": true,
37-
"esnext": true
50+
"esnext": true,
51+
"unused": true
3852
}

0 commit comments

Comments
 (0)