可视化之-JSON- function 二次绑定

function 的 二次绑定

前言

在 做 可视化的时候 ,我们的页面展示 全部是 是由JSON 处理来的 而组件 的 方法 都是写在组件内的 ,在布局复杂的JSON 里面去单独找方法 找数据,代码容易混乱

结论 :事件系统 来进行 统一处理

核心难点

  • 遍历数据中的所有function
  • function 参数获取
  • 插入 自定义 function
// 1. 遍历所有的function

 const treeDay = (list, obj, id) => {
        id = obj.id || id;
        const type = Object.prototype.toString.call(list);
        // 判断简单类型
        const isSimpleType = (type) => {
          return (
            ['[object String]', '[object Number]', '[object Boolean]'].indexOf(
              type,
            ) > -1
          );
        };
        // 判断是否是 简单类型
        if (isSimpleType(type)) return list;

        if (Array.isArray(list)) {
          list = list.map((c) => {
            return treeDay(c, c, id);
          });
        } else if (type === '[object Object]') {
          const newList = {};
          Object.keys(list).forEach((c) => {
            const eb = list[c];
            newList[c] = isSimpleType(Object.prototype.toString.call(eb))
              ? eb
              : treeDay(eb, list, id);
          });
          list = newList;
        } else if (typeof list === 'function') {
          // 设置 别名
          let er = getFunParam(list);
          const nlist = Object.assign({ fun: list });
          list = function () {
            er = Array.prototype.slice.call(arguments);
            console.log(list.name, er, id);
            return nlist.fun(...er);
          };
        }

        return list;
      };

      return treeDay(comps, {}, 'page');

// 2. 获取 function 的 参数

 const getFunParam = (fn) => {
        return /\(\s*([\s\S]*?)\s*\)/.exec(fn)[1].split(/\s*,\s*/);
 };

// 3. 插入自定义function

 		let er = getFunParam(list);
        
        const nlist = Object.assign({ fun: list });
        list = function () {
            er = Array.prototype.slice.call(arguments);
            console.log(list.name, er, id);
            return nlist.fun(...er);
         };

demo:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值