From bded9b2b00b9102ad7d0123b34c25ec9793be442 Mon Sep 17 00:00:00 2001 From: guy Date: Tue, 24 Nov 2020 17:45:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E9=80=9A=E8=BF=87=E5=B1=9E=E6=80=A7=E4=BC=A0=E9=80=92?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 1 + src/core/__test__/context.test.js | 2 +- src/core/__test__/widget.test.js | 38 +++++++++++++++++++++++++++++++ src/core/widget.js | 12 ++++++---- 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/core/__test__/widget.test.js diff --git a/changelog.md b/changelog.md index 70356883d3..c0e5a4e275 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,6 @@ # 更新日志 2.0(2020-11) +- 生命周期可以通过属性传递来操作 - 修复了颜色选择器hex框不能输入为空的问题 - 增加纯文本组件bi.pure_text - store支持webworker,引入多线程机制 diff --git a/src/core/__test__/context.test.js b/src/core/__test__/context.test.js index 9dd7414c97..8038fa9ebc 100644 --- a/src/core/__test__/context.test.js +++ b/src/core/__test__/context.test.js @@ -1,5 +1,5 @@ /** - * Created by windy on 2018/01/23. + * Created by guy on 2018/01/23. */ describe("contextTest", function () { diff --git a/src/core/__test__/widget.test.js b/src/core/__test__/widget.test.js new file mode 100644 index 0000000000..2c3d60183c --- /dev/null +++ b/src/core/__test__/widget.test.js @@ -0,0 +1,38 @@ +/** + * Created by guy on 2018/01/23. + */ +describe("widgetTest", function () { + + before(function () { + }); + + /** + * test_author_guy + */ + it("widget生命周期测试", function () { + + var Demo = BI.inherit(BI.Widget, { + render: function () { + return { + type: "bi.label", + text: "old" + }; + } + }); + BI.shortcut("demo.demo", Demo); + + var demo = BI.Test.createWidget({ + type: "demo.demo", + render: function () { + return { + type: "bi.label", + text: "new" + }; + } + }); + + expect(demo.element.text()).to.equal("new"); + demo.destroy(); + }); + +}); diff --git a/src/core/widget.js b/src/core/widget.js index 6d8cb39290..4e9abe7fde 100644 --- a/src/core/widget.js +++ b/src/core/widget.js @@ -8,8 +8,9 @@ !(function () { function callLifeHook (self, life) { - if (self[life]) { - var hooks = BI.isArray(self[life]) ? self[life] : [self[life]]; + var hook = self.options[life] || self[life]; + if (hook) { + var hooks = BI.isArray(hook) ? hook : [hook]; BI.each(hooks, function (i, hook) { hook.call(self); }); @@ -84,9 +85,9 @@ }, _initRender: function () { - if (this.beforeInit) { + if (this.options.beforeInit || this.beforeInit) { this.__asking = true; - this.beforeInit(BI.bind(this._render, this)); + (this.options.beforeInit || this.beforeInit)(BI.bind(this._render, this)); if (this.__asking === true) { this.__async = true; } @@ -184,7 +185,8 @@ _initElement: function () { var self = this; - var els = this.render && this.render(); + var render = this.options.render || this.render; + var els = render && render.call(this); if (BI.isPlainObject(els)) { els = [els]; }