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: add document for built-in objects #914

Merged
merged 8 commits into from
May 17, 2017
Merged

docs: add document for built-in objects #914

merged 8 commits into from
May 17, 2017

Conversation

dead-horse
Copy link
Member

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

@mention-bot
Copy link

@dead-horse, thanks for your PR! By analyzing the history of the files in this pull request, we identified @popomore, @atian25 and @huacnlee to be potential reviewers.

@codecov
Copy link

codecov bot commented May 16, 2017

Codecov Report

Merging #914 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #914   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          28     28           
  Lines         669    669           
=====================================
  Hits          669    669

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02b02e0...7ad50ad. Read the comment docs.

@@ -0,0 +1,227 @@
title: 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 Author

Choose a reason for hiding this comment

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

我把 egg 改成框架吧

title: egg 内置基础对象
---

在往下阅读之前,我们先初步介绍一下框架中内置的一些基础对象,包括从 [koa] 继承而来的 4 个对象(Application, Context, Request, Response) 以及框架扩展的一些对象(Helper, Controller, Service),在后续的文档阅读中我们会经常遇到它们。
Copy link
Member

Choose a reason for hiding this comment

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

Koa


## Context

Context 是一个**请求级别的对象**,继承自 [koa.Context]。在每一次收到用户请求时,框架会实例化一个 Context 对象,这个对象封装了这次用户请求的信息,并提供了许多便捷的方法来获取请求参数或者设置响应信息。框架会将所有的 [service](./service.md) 挂载到 Context 实例上,一些插件也会将一些其他的方法和对象挂载到它上面([egg-sequelize] 会将所有的 model 挂载在 Context 上。
Copy link
Member

Choose a reason for hiding this comment

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

[Service]

return class UserController extends app.Controller {
fetch* () {
const id = this.request.query.id;
this.ctx.response.body = this.app.cache.get(id);
Copy link
Member

Choose a reason for hiding this comment

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

这里感觉可以稍微提下 this.ctx.bodythis.ctx.response.body 等价,然后顺手提下 this.request.body 的区别。

Copy link
Member

Choose a reason for hiding this comment

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

req 和 res 要提下么?

Copy link
Member Author

Choose a reason for hiding this comment

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

req 和 res 先不提了

// app/controller/user.js

// 从 egg 上获取
module.exports = UserController extends require('egg').Controller {
Copy link
Member

Choose a reason for hiding this comment

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

  • require 另起一行吧。
  • 注明下推荐哪种?


Helper 自身是一个类,有和 [Controller](#controller) 基类一样的属性,它也会在每次请求时进行实例化,因此 Helper 上的所有函数也能获取到当前请求相关的上下文信息。

### 获取方式
Copy link
Member

Choose a reason for hiding this comment

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

增加在模板里面使用 Helper 的说明?

title: egg 内置基础对象
---

在往下阅读之前,我们先初步介绍一下框架中内置的一些基础对象,包括从 [koa] 继承而来的 4 个对象(Application, Context, Request, Response) 以及框架扩展的一些对象(Helper, Controller, Service),在后续的文档阅读中我们会经常遇到它们。
Copy link
Member

Choose a reason for hiding this comment

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

Config 对象要提到么?

Copy link
Member Author

Choose a reason for hiding this comment

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

感觉 config 没有什么特殊的, 在配置那一章节中已经描述好了

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 Author

Choose a reason for hiding this comment

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

本来还准备加上 config 和 logger 的

Copy link
Member Author

Choose a reason for hiding this comment

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

我等下都加上吧

@dead-horse
Copy link
Member Author

@atian25 再看看吧

Copy link
Member

@atian25 atian25 left a comment

Choose a reason for hiding this comment

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

其他 +1,写的很赞

@@ -26,6 +26,7 @@ guide_toc:
Get Start: 快速入门
Basics: 基础功能
Structure: 目录结构
Objects: 内置对象
Copy link
Member

Choose a reason for hiding this comment

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

这篇有点 Concept 的感觉 😍


### 获取方式

最常见的 context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Contenxt 实例则和 [koa] 框架在中间件中获取 Context 对象的方式一致。
Copy link
Member

Choose a reason for hiding this comment

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

第一个 context 没大写,第二个拼写错误

fetch* () {
const id = this.request.query.id;
const user = this.app.cache.get(id);
this.body = this.helper.formatUser(user);
Copy link
Member

Choose a reason for hiding this comment

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

this -> ctx


## Config

我们推荐应用开发遵循配置和代码分离的原则,将一些需要硬编码的业务配置都放到配置文件中,同时配置文件支持各个不同的运行环境使用不同的配置,使用起来也非常方便,所有框架、插件和应用级别的配置都可以通过 Config 对象获取到,关于框架的配置,可以详细阅读[Config 配置](./config.md)章节。
Copy link
Member

Choose a reason for hiding this comment

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

[Config 配置] 前面少个空格

// app/controller/user.js
module.exports = app => {
return class UserController extends app.Controller {
fetch* () {
Copy link
Member

Choose a reason for hiding this comment

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

* fetch()

Copy link
Member

Choose a reason for hiding this comment

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

@dead-horse 没改全,搜索下

@dead-horse
Copy link
Member Author

fixed

// app/controller/user.js
module.exports = app => {
return class UserController extends app.Controller {
fetch* () {
Copy link
Member

Choose a reason for hiding this comment

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

@dead-horse 没改全,搜索下


### 获取方式

最常见的 Context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Contenxt 实例则和 [koa] 框架在中间件中获取 Context 对象的方式一致。
Copy link
Member

Choose a reason for hiding this comment

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

Contenxt 这个 typo 没改

@dead-horse
Copy link
Member Author

额,再看看

Copy link
Member

@atian25 atian25 left a comment

Choose a reason for hiding this comment

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

其他 +1,没问题

};
```

和 [koa] 一样,在 Context 对象上,可以通过 `ctx.app` 访问到 Application 对象。以上面的 Controller 文件举例:
Copy link
Member

Choose a reason for hiding this comment

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

这里的链接是不是错了,底部的 [Koa]: 是大写的


### 获取方式

最常见的 Context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Context 实例则和 [koa] 框架在中间件中获取 Context 对象的方式一致。
Copy link
Member

Choose a reason for hiding this comment

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

这个 koa 也是

@dead-horse
Copy link
Member Author

😢

@okoala
Copy link
Member

okoala commented May 17, 2017

这篇要赞~

};
```

在 Controller, Service 类中,可以通过 `this.app` 访问到 Application 对象。
Copy link
Member

Choose a reason for hiding this comment

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

可以提一下这个是 controller 基类定义的,不然分不清 this.app 和 this.ctx.app

Copy link
Member Author

Choose a reason for hiding this comment

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

改成了 在继承于 Controller, Service 基类的实例中,可以通过 this.app 访问到 Application 对象。

@fengmk2 fengmk2 merged commit fd04ea2 into master May 17, 2017
@fengmk2 fengmk2 deleted the objects branch May 17, 2017 06:55

## Context

Context 是一个**请求级别的对象**,继承自 [koa.Context]。在每一次收到用户请求时,框架会实例化一个 Context 对象,这个对象封装了这次用户请求的信息,并提供了许多便捷的方法来获取请求参数或者设置响应信息。框架会将所有的 [Service] 挂载到 Context 实例上,一些插件也会将一些其他的方法和对象挂载到它上面([egg-sequelize] 会将所有的 model 挂载在 Context 上。
Copy link
Contributor

Choose a reason for hiding this comment

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

[egg-sequelize] 会将所有的 model 挂载在 Context 上

实际不是这样的

ctx.model.User 而不是直接挂的

Copy link
Member Author

Choose a reason for hiding this comment

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

最终都是�通过 ctx.model 来访问,和 service 一样吧?


最常见的 Context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Context 实例则和 [Koa] 框架在中间件中获取 Context 对象的方式一致。

框架的 [Middleware] 同时支持 koa v1 和 koa v2 两种不同的中间件写法,根据不同的写法,获取 Context 实例的方式也稍有不同:
Copy link
Contributor

Choose a reason for hiding this comment

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

koa -> Koa


## Request & Response

Request 是一个**请求级别的对象**,继承自 [koa.Request]。封装了 node 原生的 http request 对象,提供了一系列辅助方法获取 HTTP 请求常用参数。
Copy link
Contributor

Choose a reason for hiding this comment

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

node -> Node.js

http request -> HTTP Request

Copy link
Contributor

Choose a reason for hiding this comment

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

往后的类似地方也是需要修改

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

7 participants