Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里还涉及到几个地方的关联修改:

  • view 插件规范文档
  • egg-view-nunjucks 插件里面的基类也要改为 promise base 的, break change 了吧

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

喔, 记错了, 插件里面就是 promise .

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩,基类我看都是 promise base

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文档中的 * render() 改掉了

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 的注入。

有兴趣的同学可以看下对应的源码:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面的 app.View 那段好像不准确了吧? @popomore 貌似改过了.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哪一段?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那个另起 PR 改吧,这次只改 generator function 相关的

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- [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