Skip to content

Commit

Permalink
feat: [BREAKING_CHANGE] reimplement view, use egg-view plugin (eggjs#402
Browse files Browse the repository at this point in the history
)
  • Loading branch information
popomore authored and dead-horse committed Feb 23, 2017
1 parent b781db1 commit 3ba38b1
Show file tree
Hide file tree
Showing 32 changed files with 284 additions and 326 deletions.
1 change: 1 addition & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
'eslint-config-egg',
'egg-bin',
'egg-plugin-puml',
'egg-view-nunjucks',
],
keep: [
],
Expand Down
57 changes: 4 additions & 53 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ const ContextLogger = require('egg-logger').EggContextLogger;
const Cookies = require('egg-cookies');
const co = require('co');
const ContextHttpClient = require('../../lib/core/context_httpclient');
const createView = require('../../lib/core/view');
const util = require('../../lib/core/util');
const { assign } = require('utility');


const HELPER = Symbol('Context#helper');
const VIEW = Symbol('Context#view');
const LOCALS = Symbol('Context#locals');
const LOCALS_LIST = Symbol('Context#localsList');
const COOKIES = Symbol('Context#cookies');
Expand Down Expand Up @@ -127,54 +126,6 @@ const proto = module.exports = {
return this.getLogger('coreLogger');
},

/**
* View instance that is created every request
* @return {View} view
*/
get view() {
if (!this[VIEW]) {
this[VIEW] = createView(this);
}
return this[VIEW];
},

/**
* render for template path
* @method Context#render
* @param {String} name - template path
* @param {Object} [locals] - locals
* @return {Promise} resolve when render completed
*/
render(name, locals) {
return this.renderView(name, locals).then(body => {
this.body = body;
});
},

/**
* render for template path, but return string rather than writing to response
* @method Context#renderView
* @param {String} name - template path
* @param {Object} [locals] - locals
* @return {Promise} resolve html string
* @see View#render
*/
renderView(name, locals) {
return this.view.render(name, locals);
},

/**
* render for string
* @method Context#renderString
* @param {String} tpl - template string
* @param {Object} [locals] - locals
* @return {Promise} resolve html string
* @see View#renderString
*/
renderString(tpl, locals) {
return this.view.renderString(tpl, locals);
},

/**
* locals is an object for view, you can use `app.locals` and `ctx.locals` to set variables,
* which will be used as data when view is rendering.
Expand Down Expand Up @@ -204,10 +155,10 @@ const proto = module.exports = {
*/
get locals() {
if (!this[LOCALS]) {
this[LOCALS] = util.assign({}, this.app.locals);
this[LOCALS] = assign({}, this.app.locals);
}
if (this[LOCALS_LIST] && this[LOCALS_LIST].length) {
util.assign(this[LOCALS], this[LOCALS_LIST]);
assign(this[LOCALS], this[LOCALS_LIST]);
this[LOCALS_LIST] = null;
}
return this[LOCALS];
Expand Down
11 changes: 11 additions & 0 deletions config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,15 @@ module.exports = {
enable: true,
package: 'egg-jsonp',
},

/**
* view plugin
* @member {Function} Plugin#view
* @property {Boolean} enable - `true` by default
* @since 1.0.0
*/
view: {
enable: true,
package: 'egg-view',
},
};
4 changes: 2 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const graceful = require('graceful');
const http = require('http');
const EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;
const util = require('./core/util');
const cluster = require('cluster-client');
const { assign } = require('utility');

const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
Expand Down Expand Up @@ -74,7 +74,7 @@ class Application extends EggApplication {
this[LOCALS] = {};
}
if (this[LOCALS_LIST] && this[LOCALS_LIST].length) {
util.assign(this[LOCALS], this[LOCALS_LIST]);
assign(this[LOCALS], this[LOCALS_LIST]);
this[LOCALS_LIST] = null;
}
return this[LOCALS];
Expand Down
25 changes: 0 additions & 25 deletions lib/core/util.js

This file was deleted.

99 changes: 0 additions & 99 deletions lib/core/view.js

This file was deleted.

33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
],
"dependencies": {
"accepts": "^1.3.3",
"agentkeepalive": "^3.0.0",
"cluster-client": "^1.1.0",
"agentkeepalive": "^3.1.0",
"cluster-client": "^1.3.0",
"co": "^4.6.0",
"debug": "^2.6.1",
"delegates": "^1.0.0",
"egg-cluster": "^1.3.0",
"egg-cookies": "^2.1.0",
"egg-core": "^2.0.0",
"egg-cluster": "^1.5.0",
"egg-cookies": "^2.2.0",
"egg-core": "^2.1.1",
"egg-development": "^1.2.0",
"egg-i18n": "^1.1.0",
"egg-jsonp": "^1.0.0",
Expand All @@ -30,31 +30,33 @@
"egg-multipart": "^1.1.0",
"egg-onerror": "^1.3.0",
"egg-schedule": "^2.3.0",
"egg-security": "^1.4.0",
"egg-security": "^1.6.0",
"egg-session": "^1.1.0",
"egg-static": "^1.1.0",
"egg-static": "^1.2.0",
"egg-userrole": "^1.1.1",
"egg-userservice": "^1.0.0",
"egg-validate": "^1.0.0",
"egg-watcher": "^2.0.0",
"egg-view": "^1.0.0",
"egg-watcher": "^2.1.0",
"graceful": "^1.0.1",
"is-type-of": "^1.0.0",
"koa-bodyparser": "^2.3.0",
"koa-is-json": "^1.0.0",
"mime-types": "^2.1.14",
"sendmessage": "^1.1.0",
"urllib": "^2.20.0"
"urllib": "^2.20.0",
"utility": "^1.11.0"
},
"devDependencies": {
"autod": "^2.7.1",
"autod-egg": "^1.0.0",
"coffee": "^3.3.0",
"egg-alinode": "^1.0.3",
"egg-bin": "^2.0.2",
"egg-mock": "^3.0.0",
"egg-bin": "^2.2.1",
"egg-mock": "^3.0.1",
"egg-plugin-puml": "^1.0.0",
"egg-view-nunjucks": "^0.5.0",
"eslint": "^3.15.0",
"egg-view-nunjucks": "^2.0.0",
"eslint": "^3.16.0",
"eslint-config-egg": "^3.2.0",
"formstream": "^1.1.0",
"gh-pages": "^0.12.0",
Expand All @@ -68,10 +70,11 @@
"once": "^1.3.3",
"pedding": "^1.1.0",
"rds": "^0.1.0",
"rimraf": "^2.5.4",
"rimraf": "^2.6.0",
"runscript": "^1.2.0",
"should": "^11.2.0",
"supertest": "^3.0.0"
"supertest": "^3.0.0",
"taffydb": "^2.7.3"
},
"main": "index.js",
"files": [
Expand Down
35 changes: 0 additions & 35 deletions test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,41 +148,6 @@ describe('test/app/extend/context.test.js', () => {
});
});

describe('ctx.view', () => {
let app;
before(() => {
app = utils.cluster({
baseDir: 'apps/view',
customEgg: utils.getFilepath('apps/view-framework'),
});
return app.ready();
});
after(() => app.close());

it('should render template', () => {
return request(app.callback())
.get('/')
.expect(200)
.expect('name=index.html, a=111, b=b, c=testHelper');
});

it('should render string', () => {
return request(app.callback())
.get('/string')
.expect(200)
.expect('tpl={{a}}, a=111, b=b, c=testHelper');
});

it('should ctx.view === ctx.view', () => {
return request(app.callback())
.get('/sameView')
.expect(200)
.expect({
same: true,
});
});
});

describe('ctx.locals', () => {
let app;
before(() => {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/apps/i18n/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.view = {
defaultViewEngine: 'nunjucks',
};
2 changes: 1 addition & 1 deletion test/fixtures/apps/i18n/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = {
i18n: true,
view: {
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
}
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/apps/multiple-view-engine/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = app => {
app.view.use('ejs', require('./ejs'));
app.view.use('nunjucks', require('./nunjucks'));
};
Loading

0 comments on commit 3ba38b1

Please sign in to comment.