From a3d7b259cb2faf01ede0baa337d27414fb7c8472 Mon Sep 17 00:00:00 2001 From: windy <1374721899@qq.com> Date: Fri, 27 Nov 2020 14:53:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0JIRA=E4=BB=BB=E5=8A=A1=20BI.config?= =?UTF-8?q?=E7=9A=84=E5=86=85=E5=AE=B9=E5=9C=A8=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/inject.js | 105 +++++++++++++++++++++++++++---------------- src/core/shortcut.js | 12 +++++ src/core/widget.js | 12 ++--- 3 files changed, 85 insertions(+), 44 deletions(-) diff --git a/src/core/inject.js b/src/core/inject.js index 6cfa3abe1..0e0676375 100644 --- a/src/core/inject.js +++ b/src/core/inject.js @@ -49,47 +49,57 @@ var configFunctions = {}; BI.config = BI.config || function (type, configFn, opt) { - if (BI.initialized) { - if (constantInjection[type]) { - return (constantInjection[type] = configFn(constantInjection[type])); - } - if (providerInjection[type]) { - if (!providers[type]) { - providers[type] = new providerInjection[type](); - } - // 如果config被重新配置的话,需要删除掉之前的实例 - if (providerInstance[type]) { - delete providerInstance[type]; - } - return configFn(providers[type]); - } - return BI.Plugin.configWidget(type, configFn, opt); - } if (!configFunctions[type]) { configFunctions[type] = []; - BI.prepares.push(function () { - var queue = configFunctions[type]; - for (var i = 0; i < queue.length; i++) { - if (constantInjection[type]) { - constantInjection[type] = queue[i](constantInjection[type]); - continue; - } - if (providerInjection[type]) { - if (!providers[type]) { - providers[type] = new providerInjection[type](); - } - if (providerInstance[type]) { - delete providerInstance[type]; - } - queue[i](providers[type]); - continue; - } - BI.Plugin.configWidget(type, queue[i]); - } - configFunctions[type] = null; - }); } - configFunctions[type].push(configFn); + configFunctions[type].push({fn: configFn, args: opt}); + // if (BI.initialized) { + // if (constantInjection[type]) { + // return (constantInjection[type] = configFn(constantInjection[type])); + // } + // if (providerInjection[type]) { + // if (!providers[type]) { + // providers[type] = new providerInjection[type](); + // } + // // 如果config被重新配置的话,需要删除掉之前的实例 + // if (providerInstance[type]) { + // delete providerInstance[type]; + // } + // return configFn(providers[type]); + // } + // return BI.Plugin.configWidget(type, configFn, opt); + // } + // if (!configFunctions[type]) { + // configFunctions[type] = []; + // BI.prepares.push(function () { + // var queue = configFunctions[type]; + // for (var i = 0; i < queue.length; i++) { + // if (constantInjection[type]) { + // constantInjection[type] = queue[i](constantInjection[type]); + // continue; + // } + // if (providerInjection[type]) { + // if (!providers[type]) { + // providers[type] = new providerInjection[type](); + // } + // if (providerInstance[type]) { + // delete providerInstance[type]; + // } + // queue[i](providers[type]); + // continue; + // } + // BI.Plugin.configWidget(type, queue[i]); + // } + // configFunctions[type] = null; + // }); + // } + // configFunctions[type].push(configFn); + }; + + BI.Configs = BI.Configs || { + getConfig: function (type) { + return configFunctions[type]; + } }; var actions = {}; @@ -146,7 +156,16 @@ BI.Constants = BI.Constants || { getConstant: function (type) { - return constantInjection[type]; + var instance = constantInjection[type]; + BI.each(configFunctions[type], function (i, cf) { + var res = cf.fn(instance); + if (res) { + instance = res; + } + }); + constantInjection[type] = instance; + configFunctions[type] && (configFunctions[type] = null); + return instance; } }; @@ -235,9 +254,17 @@ if (!providers[type]) { providers[type] = new providerInjection[type](); } + var instance = providers[type]; + BI.each(configFunctions[type], function (i, cf) { + if (providerInstance[type]) { + delete providerInstance[type]; + } + cf.fn(instance); + }); if (!providerInstance[type]) { providerInstance[type] = new (providers[type].$get())(config); } + configFunctions[type] && (configFunctions[type] = null); return providerInstance[type]; } }; diff --git a/src/core/shortcut.js b/src/core/shortcut.js index 001a73b8c..204e498cc 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -32,6 +32,16 @@ return widget; }; + function configWidget (type) { + var configFunctions = BI.Configs.getConfig(type); + if (configFunctions) { + BI.each(configFunctions[type], function (i, cf) { + BI.Plugin.configWidget(type, cf.fn, cf.args); + }); + configFunctions[type] && (configFunctions[type] = null); + } + } + BI.createWidget = BI.createWidget || function (item, options, context, lazy) { // 先把准备环境准备好 BI.init(); @@ -53,6 +63,7 @@ } if (item.type || options.type) { el = BI.extend({}, options, item); + configWidget(el.type); w = BI.Plugin.getWidget(el.type, el); w.listeners = (w.listeners || []).concat([{ eventName: BI.Events.MOUNT, @@ -64,6 +75,7 @@ } if (item.el && (item.el.type || options.type)) { el = BI.extend({}, options, item.el); + configWidget(el.type); w = BI.Plugin.getWidget(el.type, el); w.listeners = (w.listeners || []).concat([{ eventName: BI.Events.MOUNT, diff --git a/src/core/widget.js b/src/core/widget.js index fc6d28957..521ca9101 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).call(this, 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]; }