From 59b47a23f0b2eca95d26da2b99eee4391e609842 Mon Sep 17 00:00:00 2001 From: shdong <630434698@qq.com> Date: Wed, 29 Nov 2017 15:24:49 +0800 Subject: [PATCH] fix:customDeferredGetConf (#940) * fix:customDeferredGetConf * version patch * style update --- src/mip-custom/dom.js | 19 ++++++++++++++++++- src/mip-custom/mip-custom.js | 21 ++++++++++++++++++++- src/mip-custom/package.json | 2 +- src/mip-custom/url.js | 4 ++-- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/mip-custom/dom.js b/src/mip-custom/dom.js index 46f3930db..967e0574a 100644 --- a/src/mip-custom/dom.js +++ b/src/mip-custom/dom.js @@ -285,9 +285,26 @@ define(function (require) { }); } + + /** + * [getConfigScriptElement 获取页面配置的content内容] + * 不在此做解析 + * + * @param {HTMLElement} elem mip-custom element 节点 + * @return {HTMLScriptElement} 返回`application/json`的script配置节点 + */ + function getConfigScriptElement(elem) { + if (!elem) { + return; + } + return elem.querySelector('script[type="application/json"]'); + } + + return { render: render, - proxyLink: proxyLink + proxyLink: proxyLink, + getConfigScriptElement: getConfigScriptElement }; }); diff --git a/src/mip-custom/mip-custom.js b/src/mip-custom/mip-custom.js index 5c7a0ae77..8b3a624eb 100644 --- a/src/mip-custom/mip-custom.js +++ b/src/mip-custom/mip-custom.js @@ -28,11 +28,30 @@ define(function () { }; /** - * build钩子, 定制化渲染的主流程:分区请求+渲染 + * build钩子,触发渲染 * */ customElement.prototype.build = function () { var me = this; + var checkElement = function () { + if (dom.getConfigScriptElement(me.element)) { + me.initCustom(); + return true; + } + return false; + }; + + if (!checkElement()) { + window.requestAnimationFrame(checkElement); + } + }; + + /** + * 定制化渲染的主流程:分区请求+渲染 + * + */ + customElement.prototype.initCustom = function () { + var me = this; // 初始化 me.initBuild(); diff --git a/src/mip-custom/package.json b/src/mip-custom/package.json index 7719cf836..3a1a9bb73 100644 --- a/src/mip-custom/package.json +++ b/src/mip-custom/package.json @@ -1,6 +1,6 @@ { "name": "mip-custom", - "version": "1.2.0", + "version": "1.2.1", "description": "定制化MIP加载器组件", "contributors": [ { diff --git a/src/mip-custom/url.js b/src/mip-custom/url.js index ed1c6cdef..eadd6313e 100644 --- a/src/mip-custom/url.js +++ b/src/mip-custom/url.js @@ -8,7 +8,7 @@ define(function (require) { * @type {Object} */ var util = require('util'); - + var dom = require('mip-custom/dom'); var data = require('mip-custom/data'); /** @@ -41,7 +41,7 @@ define(function (require) { // 获取用户设置参数,获取不到则报错并返回 try { - var script = element.querySelector('script[type="application/json"]'); + var script = dom.getConfigScriptElement(element); if (script) { userParams = JSON.parse(script.textContent); if (!userParams.accid) {