diff --git a/src/core/inject.js b/src/core/inject.js index 6cfa3abe1e..89d0bd9933 100644 --- a/src/core/inject.js +++ b/src/core/inject.js @@ -49,47 +49,16 @@ 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}); + }; + + BI.Configs = BI.Configs || { + getConfig: function (type) { + return configFunctions[type]; + } }; var actions = {}; @@ -146,7 +115,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 +213,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 001a73b8cc..204e498cc6 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 fc6d289579..521ca9101e 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]; }