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

support koa ctx.state #10

Merged
merged 2 commits into from
May 30, 2015
Merged

support koa ctx.state #10

merged 2 commits into from
May 30, 2015

Conversation

imsobear
Copy link
Contributor

koa 文档:

ctx.state

The recommended namespace for passing information through middleware and to your frontend views.

类似 express 的 locals, 可以在 view 里直接使用的变量,如 user, 而无需每次 render 的时候都需要传入。

@coveralls
Copy link

Coverage Status

Coverage remained the same at 76.19% when pulling 764b407 on imsobear:master into c039a84 on xtemplate:master.

@yiminghe
Copy link
Member

@dead-horse

*/
function merge(target, source) {
for (var key in source) {
target[key] = source[key];
Copy link
Member

Choose a reason for hiding this comment

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

if (!target.hasOwnProperty(key)) {
  target[key] = source[key];
}

this.state 不应该覆盖 data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

额,我直接从 koa-ejs 复制的,我改下,没仔细看…

Copy link
Member

Choose a reason for hiding this comment

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

  app.context.render = function *(view, _context) {
    var context = {};
    merge(context, this.state);
    merge(context, _context);

这样吧,不改变原来的 data 对象,而且用 data 覆盖 state 的内容

@coveralls
Copy link

Coverage Status

Coverage remained the same at 76.19% when pulling 176fe8c on imsobear:master into c039a84 on xtemplate:master.

@imsobear
Copy link
Contributor Author

不要在意测试用例的端口号从 9000 -> 4001 了,逃~~~

dead-horse added a commit that referenced this pull request May 30, 2015
@dead-horse dead-horse merged commit 5660eaa into xtemplate:master May 30, 2015
@dead-horse
Copy link
Member

没权限,@yiminghe 发布一下吧

@yiminghe
Copy link
Member

yiminghe commented Jun 1, 2015

done. 权限加了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants