Skip to content

Commit

Permalink
docs: improve framework doc based on eggjs/examples#9
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Jan 16, 2017
1 parent ecbed08 commit d846290
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions docs/source/zh-cn/advanced/framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ startCluster({
"name": "yadan",
"dependencies": {
"egg": "^1.0.0"
},
"devDependencies": {
"egg-bin": "^1.0.0"
},
"scripts": {
"dev": "egg-bin dev"
}
}

Expand All @@ -102,26 +96,29 @@ exports.Application = require('./lib/application.js');
const path = require('path');
const Application = require('egg').Application;
const EGG_PATH = Symbol.for('egg#eggPath');
class ExampleApplication extends Application {
class YadanApplication extends Application {
get [EGG_PATH]() {
// 返回 framework 路径
return path.dirname(__dirname);
}
}
module.exports = ExampleApplication;
module.exports = YadanApplication;
```

启动时需要指定 example 找到他的 Application,指定 `--framework` 从 node_modules 找指定模块作为框架,默认会使用 egg。
启动时需要指定框架名找到他 export 的 Application,`package.json` 指定 `egg.framework` 从 node_modules 找指定模块作为框架,默认会使用 egg。

```json
{
"scripts": {
"dev": "egg-bin dev --framework example"
"dev": "egg-bin dev"
},
"egg": {
"framework": "yadan"
}
}
```

现在 exmaple 目录已经是一个 loadUnit,那么相应目录的文件都会被加载,查看[框架被加载的文件](./loader.md)
现在 yadan 框架目录已经是一个 loadUnit,那么相应目录和文件(如 `app``config`)都会被加载,查看[框架被加载的文件](./loader.md)

### 框架继承原理

Expand Down Expand Up @@ -171,16 +168,16 @@ exports.Agent = require('./lib/agent.js');
const path = require('path');
const Agent = require('egg').Agent;
const EGG_PATH = Symbol.for('egg#eggPath');
class ExampleAgent extends Agent {
class YadanAgent extends Agent {
get [EGG_PATH]() {
// 返回 framework 路径
return path.dirname(__dirname);
}
}
module.exports = ExampleAgent;
module.exports = YadanAgent;
```

但因为 Agent 和 Application 是两个实例,所以 API 有可能不一致。
**但因为 Agent 和 Application 是两个实例,所以 API 有可能不一致。**

### 自定义 Loader

Expand All @@ -196,28 +193,28 @@ exports.AppWorkerLoader = require('./lib/app_worker_loader.js');
// lib/application.js
const path = require('path');
const Application = require('egg').Application;
const ExampleAppWorkerLoader = require('./app_worker_loader');
const AppWorkerLoader = require('./app_worker_loader');
const EGG_PATH = Symbol.for('egg#eggPath');
const EGG_LOADER = Symbol.for('egg#loader');
class ExampleApplication extends Application {
class YadanApplication extends Application {
get [EGG_PATH]() {
return path.dirname(__dirname);
}
// 覆盖 egg 的 Loader,启动时使用这个 Loader
get [EGG_LOADER]() {
return ExampleAppWorkerLoader;
return AppWorkerLoader;
}
}
module.exports = ExampleApplication;
module.exports = YadanApplication;

// lib/app_worker_loader.js
class ExampleAppWorkerLoader extends AppWorkerLoader {
class YadanAppWorkerLoader extends AppWorkerLoader {
load() {
super.load();
// 自己扩展
}
}
module.exports = ExampleAppWorkerLoader;
module.exports = YadanAppWorkerLoader;
```

AgentWorkerLoader 扩展也类似,这里不再举例。AgentWorkerLoader 加载的文件可以于 AppWorkerLoader 不同,比如 默认加载时,egg 的 AppWorkerLoader 会加载 `app.js` 而 AgentWorkerLoader 加载的是 `agent.js`
Expand Down

0 comments on commit d846290

Please sign in to comment.