Skip to content

Commit

Permalink
feat: view base promise to support async function (eggjs#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and popomore committed Feb 9, 2017
1 parent 6dd9feb commit c6914e0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
19 changes: 11 additions & 8 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,36 @@ const proto = module.exports = {
* @method Context#render
* @param {String} name - template path
* @param {Object} [locals] - locals
* @return {Promise} resolve when render completed
*/
* render(name, locals) {
this.body = yield this.renderView(name, locals);
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 {String} html string
* @return {Promise} resolve html string
* @see View#render
*/
* renderView(name, locals) {
return yield this.view.render(name, locals);
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 {String} html string
* @return {Promise} resolve html string
* @see View#renderString
*/
* renderString(tpl, locals) {
return yield this.view.renderString(tpl, locals);
renderString(tpl, locals) {
return this.view.renderString(tpl, locals);
},

/**
Expand Down
4 changes: 2 additions & 2 deletions docs/source/zh-cn/advanced/view-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ function loadFilter() {
- 该子类会在原 render 方法的基础上,增加对 locals 的注入。

有兴趣的同学可以看下对应的源码:
- [app/extend/context.js](https://github.com/eggjs/egg/blob/master/app/extend/context.js), `* render()`
- [app/extend/context.js](https://github.com/eggjs/egg/blob/master/app/extend/context.js), `render()`
- [app/extend/application.js](https://github.com/eggjs/egg/blob/master/app/extend/application.js), `get View()`
- [lib/core/view.js](https://github.com/eggjs/egg/blob/master/lib/core/view.js)


[egg-security]: https://github.com/eggjs/egg-security
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
[egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
19 changes: 19 additions & 0 deletions test/fixtures/apps/view-render/app/controller/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};

module.exports = app => {
return class AsyncController extends app.Controller {
index() {
const ctx = this.ctx;
return __awaiter(this, void 0, void 0, function* () {
yield ctx.render('index.html', {name: 'mk・2'});
});
}
}
};
1 change: 1 addition & 0 deletions test/fixtures/apps/view-render/app/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = app => {
app.get('home', '/', app.controller.home);
app.get('async', '/async', 'async.index');
app.get('empty', '/empty', app.controller.empty);
// app.get('/only_require', app.controller.onlyRequire);
app.get('/xss', app.controller.xss);
Expand Down
7 changes: 7 additions & 0 deletions test/lib/plugins/view/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ describe('test/lib/plugins/view/render.test.js', () => {
.expect(`Hi, mk・2\ntest-app-helper: test-bar@${app.config.baseDir}\nraw: <div>dar</div>\n2014 @ mk2 &lt;br&gt;\n`, done);
});

it('should render with async function controller', function(done) {
request(app.callback())
.get('/async')
.expect(200)
.expect(`Hi, mk・2\ntest-app-helper: test-bar@${app.config.baseDir}\nraw: <div>dar</div>\n2014 @ mk2 &lt;br&gt;\n`, done);
});

it('should render have helper instance', function(done) {
request(app.callback())
.get('/')
Expand Down

0 comments on commit c6914e0

Please sign in to comment.