Chinaunix首页 | 论坛 | 博客
  • 博客访问: 53181
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
文章分类
文章存档

2016年(48)

我的朋友

分类: JavaScript

2016-10-15 17:21:41

koahubjs发布0.09 新增钩子机制
添加钩子机制,控制器钩子和函数钩子
修复自动加载bug,实现除自动加载导出的default外,还能自动加载其他的方法

记koahubjs钩子开发过程

在使用koahubjs开发项目完成之后,总是需要另外增加一些插件功能,这种情况下改动项目代码,风险太大,所以钩子机制不可缺少。koahubjs将钩子加载到了内存中,原因是因为koahubjs框架并没有内置数据库,而且加载到内存中更加灵活。
钩子1
控制器钩子
执行某个http请求的时候,想要调用另外一个控制器的方法
钩子2
方法钩子
执行某个http请求的时候,想要调用某个公共的函数
直接上代码

点击(此处)折叠或打开

  1. //创建hook.class.js
  2. // hooks 定义
  3. // {
  4. // addOrder: [
  5. // ’/admin/index/index’
  6. // ]
  7. // }
  8.  
  9. export default class {
  10.     constructor() {
  11.         this.hooks = {};
  12.     }
  13.  
  14.     get() {
  15.         return this.hooks;
  16.     }
  17.  
  18.     add(name, action) {
  19.         let add = true;
  20.         for (let key in this.hooks) {
  21.             if (name == key) {
  22.                 this.hooks[key].push(action);
  23.                 add = false;
  24.             }
  25.         }
  26.         if (add) {
  27.             this.hooks[name] = [action];
  28.         }
  29.  
  30.         return this.get();
  31.     }
  32.  
  33.     run(name) {
  34.         for (let key in this.hooks) {
  35.             if (name == key) {
  36.                 for (let path of this.hooks[key]) {
  37.                     if (/\w+\(.*\)$/.test(path)) {
  38.                         this.runFunction(path);
  39.                     } else {
  40.                         this.runController(path);
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.     }
  46.  
  47.     runController(path) {
  48.         let action = path.slice(path.lastIndexOf(/));
  49.         path = path.slice(0, path.lastIndexOf(/));
  50.  
  51.         let include = false;
  52.         for (let _key in koahub.controllers) {
  53.             if (_key == path) {
  54.                 include = true;
  55.                 break;
  56.             }
  57.         }
  58.  
  59.         if (include) {
  60.             let ctrl = koahub.controllers[path];
  61.             let pros = Object.getOwnPropertyNames(ctrl.prototype).filter(function(value) {
  62.                 if (value == ’constructor’) {
  63.                     return false;
  64.                 }
  65.                 return true;
  66.             });
  67.  
  68.             let callFlag = true;
  69.             for (let k in pros) {
  70.                 if (/+ pros[k] == action) {
  71.                     Object.getPrototypeOf(new ctrl())[pros[k]].call(this);
  72.                     callFlag = false;
  73.                 }
  74.             }
  75.  
  76.             if (callFlag) {
  77.                 console.error(’Hook Not Found Method’);
  78.             }
  79.         } else {
  80.             console.error(’Hook Not Found Controller’);
  81.         }
  82.     }
  83.  
  84.     runFunction(value) {
  85.         eval(`koahub.utils.${value}`);
  86.     }
  87. }


钩子需要提前挂载到相应的节点上

点击(此处)折叠或打开

  1. //开始挂载
  2. //controller钩子
  3. koahub.hook.add(’hook1’,/admin/public/sendEmail’);
  4. //function钩子
  5. koahub.hook.add(’hook2’, ’tools.add(1,2));
  6.  
  7. //调用钩子
  8. koahub.hook.run(’hook1’);
  9. koahub.hook.run(’hook2’);

  10. util方法
  11. //util下的*.util.js会自动挂载到koahub.utils上
  12. //util/tools.util.js
  13. export function add(a, b) {
  14.     console.log(a + b);
  15.     return a + b;
  16. }
  17.  
  18. export function dis(a, b) {
  19.     console.log(a - b);
  20.     return a - b;
  21. }


KoaHub.js – 基于 Koa.js 平台的 Node.js web 快速开发框架
安装

点击(此处)折叠或打开

  1. npm install koahubjs --save

Star Github

官网:
wemall  开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统
wemall地址:

 

阅读(374) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~