mm_tpl 2.1.9 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/hook.js +78 -2
  2. package/package.json +1 -1
  3. package/test.js +27 -14
package/hook.js CHANGED
@@ -1,5 +1,10 @@
1
+ const {
2
+ types
3
+ } = require("node:util");
4
+
1
5
  class Hook {
2
6
  constructor() {
7
+ this.dict_func = {};
3
8
  this.dict_action = {};
4
9
  this.dict_filter = {};
5
10
  }
@@ -15,6 +20,27 @@ Hook.prototype.sort = function(list) {
15
20
  });
16
21
  }
17
22
 
23
+ /**
24
+ * 添加函数钩子
25
+ * @param {String} name 钩子名称
26
+ * @param {Function} func 钩子函数
27
+ * @param {Number} sort 执行顺序
28
+ * @param {String} alias 函数别名,用于删除
29
+ */
30
+ Hook.prototype.addFunc = function(name, func, sort, alias) {
31
+ var obj = {
32
+ func,
33
+ sort,
34
+ alias
35
+ }
36
+ var arr = this.dict_func[name];
37
+ if (!arr) {
38
+ arr = [];
39
+ }
40
+ arr.push(obj);
41
+ this.dict_func[name] = this.sort(arr);
42
+ }
43
+
18
44
  /**
19
45
  * 添加动作钩子
20
46
  * @param {String} name 钩子名称
@@ -57,6 +83,21 @@ Hook.prototype.addFilter = function(name, func, sort, alias) {
57
83
  this.dict_filter[name] = this.sort(arr);
58
84
  }
59
85
 
86
+
87
+ /**
88
+ * 删除函数钩子
89
+ * @param {String} name 钩子名称
90
+ * @param {String} alias 函数别名
91
+ */
92
+ Hook.prototype.delFunc = function(name, alias) {
93
+ var arr = this.dict_func[name];
94
+ if (arr) {
95
+ arr.del({
96
+ alias
97
+ });
98
+ }
99
+ }
100
+
60
101
  /**
61
102
  * 删除动作钩子
62
103
  * @param {String} name 钩子名称
@@ -98,7 +139,11 @@ Hook.prototype.runAction = function(name, model, ...args) {
98
139
  if (list) {
99
140
  for (var i = 0; i < list.length; i++) {
100
141
  var o = list[i];
101
- ret += o.func(model, ...args);
142
+ try {
143
+ ret += o.func(model, ...args);
144
+ } catch (err) {
145
+ console.error(ret);
146
+ }
102
147
  }
103
148
  }
104
149
  return ret;
@@ -117,7 +162,38 @@ Hook.prototype.runFilter = function(name, ret, model, ...args) {
117
162
  if (list) {
118
163
  for (var i = 0; i < list.length; i++) {
119
164
  var o = list[i];
120
- ret = o.func(ret, model, ...args);
165
+ try {
166
+ ret = o.func(ret, model, ...args);
167
+ } catch (err) {
168
+ console.error(ret);
169
+ }
170
+ }
171
+ }
172
+ return ret;
173
+ }
174
+
175
+ /**
176
+ * 运行函数钩子
177
+ * @param {String} name 钩子名称
178
+ * @param {Object} model 模型
179
+ * @param {Object} req 请求参数
180
+ * @param {Object} args 附加参数
181
+ */
182
+ Hook.prototype.runFunc = async function(name, model, req, ...args) {
183
+ var ret;
184
+ var dict = this.dict_func;
185
+ var list = dict[name];
186
+ if (list) {
187
+ for (var i = 0; i < list.length; i++) {
188
+ var o = list[i];
189
+ try {
190
+ ret = o.func(model, req, ...args);
191
+ if (ret) {
192
+ ret = await ret;
193
+ }
194
+ } catch (err) {
195
+ console.error(ret);
196
+ }
121
197
  }
122
198
  }
123
199
  return ret;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_tpl",
3
- "version": "2.1.9",
3
+ "version": "2.2.0",
4
4
  "description": "这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -11,13 +11,13 @@ const Tpl = require('./index.js');
11
11
  // async function test() {
12
12
  // var tpl = new Tpl();
13
13
  // // var body = tpl.render("<a>${loop arr value idx} ${idx} ${ value } ${/loop} + 123123</a>", { arr: [123,234] });
14
-
14
+
15
15
  // var body = tpl.render("<a>${ loop obj value key } ${ key } ${ value } ${/loop} + 123123</a>", { obj: { a: 123, b: 467 } });
16
-
16
+
17
17
  // // var body = tpl.render("<a><!--[ for(var i = 0; i < arr.length; i++){ ]--> ${ arr[i] } <!--[ } ]--> + 123123</a>", {
18
18
  // // arr: [123, 234]
19
19
  // // });
20
-
20
+
21
21
  // // var body = tpl.render("<a>${ each arr } ${ arr[$index] } ${/each} + 123123</a>", { arr: [123,234] });
22
22
  // // var view = tpl.render("<a>${ each arr } ${ $value } ${/each} + 123123</a>", { arr: [123,234] });
23
23
  // console.log(body);
@@ -27,10 +27,10 @@ const Tpl = require('./index.js');
27
27
  // var tpl = new Tpl();
28
28
  // // 设置全局变量
29
29
  // $.globalBag.name = "超级美眉Art模板引擎";
30
-
30
+
31
31
  // // 设置实例化视图变量
32
32
  // tpl.viewBag.name = "系统名称";
33
-
33
+
34
34
  // // 渲染视图
35
35
  // var body = tpl.view("./test.html", { arr: [123,234] });
36
36
  // console.log(body);
@@ -38,42 +38,55 @@ const Tpl = require('./index.js');
38
38
 
39
39
  async function test() {
40
40
  var tpl = new Tpl();
41
-
41
+
42
42
  // 设置全局变量
43
43
  $.globalBag.name = "超级美眉Art模板引擎";
44
-
44
+
45
45
  // 设置实例化视图变量
46
46
  tpl.viewBag.name = "系统名称";
47
47
  tpl.current_theme = "game";
48
48
  tpl.dir = "";
49
+
50
+ $.hook.addFunc('list', async function(viewBag, req, type) {
51
+ console.log(viewBag, req, type);
52
+ });
49
53
 
54
+ await $.hook.runFunc('list', tpl.viewBag, {
55
+ query: {
56
+ a: 1,
57
+ b: 2
58
+ }
59
+ }, 'article');
60
+
50
61
  $.hook.addAction('test', function(m, param) {
51
62
  // console.log(param, m);
52
63
  return "<div>你好吗?</div>";
53
64
  }, 2, "测试动作2");
54
-
65
+
55
66
  $.hook.addAction('test', function(m, param) {
56
67
  return "<div>你好!</div>";
57
68
  }, 1, "测试动作1");
58
-
69
+
59
70
  $.hook.addFilter('test', function(ret, m, param) {
60
71
  // console.log(ret, param, m);
61
72
  return ret.replace("你", "您");
62
73
  }, 1, "测试过滤");
63
-
74
+
64
75
  // $.hook.delAction('test', "测试动作1");
65
76
  // $.hook.delFilter('test', "测试过滤");
66
-
77
+
67
78
  // 渲染视图(并缓存)
68
- var body = tpl.view("./test", { arr: [123, 234] }, {
79
+ var body = tpl.view("./test", {
80
+ arr: [123, 234]
81
+ }, {
69
82
  // 是否读取缓存文件, 如果为false, 但有cache_filename, 表示只保存缓存,但不读取
70
83
  cache: false,
71
84
  // 缓存文件名
72
85
  cache_filename: "./test"
73
86
  });
74
-
87
+
75
88
  // 删除指定目录的缓存文件
76
89
  // tpl.clear_cache('./mall');
77
90
  console.log(body);
78
91
  }
79
- test();
92
+ test();