Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: view #228

Merged
merged 1 commit into from
Jan 12, 2017
Merged

docs: view #228

merged 1 commit into from
Jan 12, 2017

Conversation

atian25
Copy link
Member

@atian25 atian25 commented Jan 10, 2017

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change


绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。

而 egg 并不强制你使用某种模板引擎,开发者可以依据下文的规范, 来封装对应的模板引擎插件。
Copy link
Member

Choose a reason for hiding this comment

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

egg => 框架

你 => 我们

Copy link
Member

Choose a reason for hiding this comment

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

而 egg 并不强制你使用某种模板引擎,开发者可以依据下文的规范, 来封装对应的模板引擎插件。

而框架并不强制使用某种模板引擎,我们可以依据下文的规范, 来封装对应的模板引擎插件。

app.locals = { a: 2 };
console.log(this.locals);

// assign directly is ok, will merge by setter, output: { a: 1, b: 1, c: 1, d: 4 }
Copy link
Member

Choose a reason for hiding this comment

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

c: 3


绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。

框架并不强制你使用某种模板引擎,并且没有内置的 view 插件,只是约定了 [view 插件开发规范](../practice/view.md),开发者需根据团队技术选型自行引入。
Copy link
Member Author

Choose a reason for hiding this comment

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

view 插件开发规范 这个 view 大写首字母还是小写好?

Copy link
Member

Choose a reason for hiding this comment

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

如果分开,开发规范就不适合放到 practice 里了

Copy link
Member

Choose a reason for hiding this comment

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

这个路径错了

Copy link
Member Author

Choose a reason for hiding this comment

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

放到 advanced ?

// config/config.default.js
exports.view = {
dir: path.join(appInfo.baseDir, 'app/view'),
// should set to `false` at local development mode
Copy link
Member

Choose a reason for hiding this comment

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

插件自己配置了 local 环境下为 false 吧?可以说明一下

Copy link
Member

Choose a reason for hiding this comment

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

这个有约定?

Copy link
Member Author

Choose a reason for hiding this comment

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

之前的 web.md 没有, 这次写文档的时候, 想加上这些实践规约, 统一命名好一点吧?

在渲染页面的过程中,我们通常需要一个变量来收集需要传递给模板的变量,在框架里面,我们提供了 `app.locals` 和 `this.locals`。

- `app.locals` 为全局的,一般在 `app.js` 里面配置全局变量。
- `this.locals` 为单次请求的,会合并 `app.locals`
Copy link
Member

Choose a reason for hiding this comment

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

中间行没有句号

{
"name": "egg-view-nunjucks",
"eggPlugin": {
"name": "view"
Copy link
Member

Choose a reason for hiding this comment

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

name 换成真实的吧

Copy link
Member Author

Choose a reason for hiding this comment

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

这是真实的,不同 view 插件都要用 view


绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。

框架并不强制你使用某种模板引擎,并且没有内置的 view 插件,只是约定了 [view 插件开发规范](../practice/view.md),开发者需根据团队技术选型自行引入。
Copy link
Member

Choose a reason for hiding this comment

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

如果分开,开发规范就不适合放到 practice 里了


1. 引入 view 插件

```bash
Copy link
Member

Choose a reason for hiding this comment

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

list 都需要缩进

Copy link
Member Author

Choose a reason for hiding this comment

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

好像看到其他的都没有缩进, 渲染后会自动缩进的吧.

这里缩进后, 下面的代码块缩进似乎不好看

Copy link
Member

Choose a reason for hiding this comment

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

不会自动的。

exports.view = {
enable: true,
package: 'egg-view-nunjucks',
}
Copy link
Member

Choose a reason for hiding this comment

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

分号

// config/config.default.js
exports.view = {
dir: path.join(appInfo.baseDir, 'app/view'),
// should set to `false` at local development mode
Copy link
Member

Choose a reason for hiding this comment

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

这个有约定?

```js
// config/config.default.js
exports.view = {
dir: path.join(appInfo.baseDir, 'app/view'),
Copy link
Member

Choose a reason for hiding this comment

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

这个有约定?

appInfo 在哪

// mount `View` class to app
// egg will create an instance to `ctx.view` at every request
// you can use `this.render` at controller
get [Symbol.for('egg#view')]() {
Copy link
Member

Choose a reason for hiding this comment

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

这里也要说明下,英文都改了

const path = require('path');

module.exports = appInfo => {
const config = {};
Copy link
Member

Choose a reason for hiding this comment

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

要不我们改成直接 return 的?

Copy link
Member Author

Choose a reason for hiding this comment

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

module.exports = appInfo => {
  return {
    /**
    * View options
    * @member Config#view
    * @property {String} dir - full path of template dir, support multiple path by using comma, defaults to `{app_root}/app/view`.
    * @property {Boolean} cache - whether cache template, default to true except false at local env.
    */
    view: {
      dir: path.join(appInfo.baseDir, 'app/view'),
      cache: true,
    }
  }
};

这样? 貌似不是很好看


### filter

模板引擎中最常用的就是 filter,故我们在框架的[扩展](../basics/extend.md)之上增加对 `app/extend/filter.js` 的支持:
Copy link
Member

Choose a reason for hiding this comment

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

filter 可以弱化,我们强调 helper

Copy link
Member Author

Choose a reason for hiding this comment

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

也要介绍到这个扩展吧? helper 比起 filter 有什么优点?

// {plugin_root}/app/extend/application.js
const helper = require('../../lib/helper');
module.exports = {
get ViewHelper() {
Copy link
Member

Choose a reason for hiding this comment

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

这里不用挂 app 了?直接 require?

Copy link
Member Author

Choose a reason for hiding this comment

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

也行, 之前是从 chair 那里拷贝的, ViewHelper 挂出来也没啥用

Copy link
Member Author

Choose a reason for hiding this comment

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

Helper 这个类还是需要缓存起来的,不在 app 就得在 View 里面。

eggjs/egg-view-nunjucks#8

@@ -0,0 +1,259 @@
title: View 插件开发规范
Copy link
Member

Choose a reason for hiding this comment

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

放到 advanced 好了

@codecov-io
Copy link

codecov-io commented Jan 12, 2017

Current coverage is 97.89% (diff: 100%)

Merging #228 into master will not change coverage

@@             master       #228   diff @@
==========================================
  Files            34         34          
  Lines           902        902          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
  Hits            883        883          
  Misses           19         19          
  Partials          0          0          

Powered by Codecov. Last update 6e24326...cc15897

const helper = require('./helper');

module.exports = class MyCustomView {
get Helper() {
Copy link
Member Author

Choose a reason for hiding this comment

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

@popomore 不在 app 定义 ViewHelper, 就得在这里缓存下. 你看哪种方式好点?

eggjs/egg-view-nunjucks#8

@atian25
Copy link
Member Author

atian25 commented Jan 12, 2017

可以 review 了.

});
```

挂载到 application :
Copy link
Member

Choose a reason for hiding this comment

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

扩展到 application

render(name, locals) {
// name 为模板文件的相对于 `app/view` 的路径
return new Promise((resolve, reject) => {
this.app.viewEngine.render(name, locals, function(err, result) {
Copy link
Member

Choose a reason for hiding this comment

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

function(err, result) { => (err, result) => {


配置的 key 为 `view`,并提供至少两个配置属性:

- {String} dir - 模板文件的根目录,绝对路径。
Copy link
Member

Choose a reason for hiding this comment

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

要说一下通过逗号分隔支持多路径的形式

Copy link
Member Author

Choose a reason for hiding this comment

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

在下面的代码示例里面增加了

$ npm i egg-view-nunjucks --save
```

2. 启用插件
Copy link
Member

Choose a reason for hiding this comment

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

1, 2, 3 不会生效的

image

Copy link
Member Author

Choose a reason for hiding this comment

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

奇怪, 在 vscode 的 markdown preview 里面是正常的

module.exports = function* home(){
const data = { name: 'egg' };

// render a template, path releate to `app/view`
Copy link
Member

Choose a reason for hiding this comment

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

relate

@fengmk2
Copy link
Member

fengmk2 commented Jan 12, 2017

没啥大问题。

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

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

LGTM

@popomore popomore mentioned this pull request Jan 12, 2017
31 tasks
@atian25
Copy link
Member Author

atian25 commented Jan 12, 2017

OK,都更新了,我这边有 2 个待确定的点, @popomore 看看:

  • 针对应用开发者的那份,要不要干掉 filter?
  • ViewHelper 的实现,挂到 application 还是 view?

@popomore
Copy link
Member

@atian25 看了下确实原来的方式好,辛苦了

}
// ...
}
return ViewHelper;
Copy link
Member

Choose a reason for hiding this comment

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

这里直接设 app.ViewHelper

Copy link
Member Author

Choose a reason for hiding this comment

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

现在那样可以延迟初始化


get helper() {
if (!this[HELPER]) {
this[HELPER] = new this.Helper(this.ctx);
Copy link
Member

Choose a reason for hiding this comment

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

this.app.ViewHelper

@atian25
Copy link
Member Author

atian25 commented Jan 12, 2017

ok, 我改回来

@popomore
Copy link
Member

只改了文档,我合并了

@popomore popomore merged commit d63752e into master Jan 12, 2017
@popomore popomore deleted the view branch January 12, 2017 10:13
mattma added a commit that referenced this pull request Jan 17, 2017
* master: (37 commits)
  docs: fix quickstart typo (#266)
  docs: add http client debug docs (#265)
  docs: modify and fix 3 points (#264)
  docs(intro): improve decription (#263)
  docs: fix docs site version (#262)
  docs: Fix typo.  (#261)
  docs: review 1st version docs (#257)
  fix: typo conext -> context (#259)
  docs: contributing && readme && deps (#253)
  docs: fix quickstart link in index.html (#256)
  docs: set the default locale zh-cn (#255)
  refactor: ctx.realStatus delegate ctx.response.realStatus (#252)
  docs: Add intro/index.md (#246)
  feat: adjust default plugins (#251)
  docs: add RESTful documents (#247)
  feat: delegate ctx.jsonp to ctx.response.jsonp (#248)
  chore: remove examples (#245)
  docs: improve mysql doc
  docs: add mysql doc
  docs: view (#228)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants