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

fix(router): support app.get(url, controllerName) #38

Merged
merged 3 commits into from
Jan 12, 2017
Merged

Conversation

dead-horse
Copy link
Member

@dead-horse dead-horse commented Jan 11, 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

closes eggjs/egg#201

@mention-bot
Copy link

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

@codecov-io
Copy link

codecov-io commented Jan 11, 2017

Current coverage is 99.40% (diff: 100%)

No coverage report found for master at 6e8de5a.

Powered by Codecov. Last update 6e8de5a...4aa93fd

@@ -1,6 +1,6 @@
module.exports = function (app) {
app.get('/locals/router', app.controller.locals.router);
app.get('member_index', '/members/index', 'members.index');
Copy link
Member

Choose a reason for hiding this comment

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

不再支持 name, path, action?

Copy link
Member Author

Choose a reason for hiding this comment

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

支持啊,那个例子和下面重复了

if (typeof controller !== 'string') return args;
const actions = controller.split('.');
let obj = app.controller;
actions.forEach(function(key) {
Copy link
Member

Choose a reason for hiding this comment

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

arrow function

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

/**
* controller(last argument) support string
* - [url, controller]: app.get('/home', 'home');
* - [name, url, controller]: app.get('home', '/home', 'home');
Copy link
Member

Choose a reason for hiding this comment

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

最后那个用 news.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.

news.list 是啥?

Copy link
Member

Choose a reason for hiding this comment

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

app.get('home', '/home', 'home'); 前后各一个 home, 要不要换个名字, 如

app.get('newsList', '/news/list', 'news.list'); 这样的? 不过这样也行了.

const actions = controller.split('.');
let obj = app.controller;
actions.forEach(key => {
obj = obj[key];
Copy link
Member

Choose a reason for hiding this comment

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

如果指定一个不存在的 controller 呢

Copy link
Member Author

Choose a reason for hiding this comment

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

加了一个 throw ,提示友好一点

actions.forEach(key => {
obj = obj[key];
});
return Array.prototype.slice.call(args, 0, -1).concat([ obj ]);
Copy link
Member

Choose a reason for hiding this comment

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

直接用 args[args.length - 1] = obj 不行吗

@@ -129,14 +138,10 @@ class Router extends KoaRouter {
const controller = middleware.pop();

for (const key in REST_MAP) {
let action = '';
if (typeof controller === 'string') {
action = `${controller}.${key}`;
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.

兼容的,这段逻辑等于是统一到下面那个方法里面了,进到这里的时候已经都解析成对象了

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.

+1

@fengmk2 fengmk2 merged commit 115b5aa into master Jan 12, 2017
@fengmk2 fengmk2 deleted the fix-router branch January 12, 2017 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

router path 解析 bug
6 participants