美文网首页
实现一个业务组件管理器

实现一个业务组件管理器

作者: jluemmmm | 来源:发表于2020-08-12 08:11 被阅读0次
/**
 * @description 业务组件管理器
 * @method registre
 * @method getConText
 * @method render
 * @method run
 * @class ComponentManager
 */
import EventEmit from 'xxx'
function noop() {}
const DEFAULTOPTION = {
  deffer: '',
  name: '',
  component: '',
  data: {},
  onReady: noop,
  onRendered: noop,
  onUpdated: noop,
  onDestoryed: noop
}
class ComponentManager {
  constructor (context = {}) {
    this.component = {}
    this.parentContext = context
  }

  registe(options = {}) {
    let el = document.querySelector(`*[component='${options.name}']`)
    if(!el) return
    //添加自定义事件通知回调
    this._analyseEvent(el)

    //实例化组件
    let comInstance = new options.component(el, options.name, typeof options.data !== 'object' ? {} : options.data)

    //注册生命周期的回调函数
    this._bindLifeCycleEvent(options)

    //初始化组件
    comInstance.init && comInstance.init()
    this.components[options.name] = {
      instance: comInstance,
      options,
      el,
      isDeffer: options.deffer
    }
    return comInstance
  }

  _bindLifeCycleEvent(options) {
    EventEmit.instance.on(`${options.name}`, options.onReady || noop)
    EventEmit.instance.on(`${options.name}`, options.onRendered || noop)
    EventEmit.instance.on(`${options.name}`, options.onUpdated || noop)
    EventEmit.instance.on(`${options.name}`, options.onUpdated || noop)
    EventEmit.instance.on(`${options.name}`, options.onDestoryed || noop)
    EventEmit.instance.on(`${options.name}`, options.onDataReady || noop)
  }

  /**
   * 解析组件沙盒上需要定义的事件名, 仅支持一个.once的修饰符
   */
  _analyseEvent(el) {
    let attrs = [...el.attriburtes]
    let props = {}
    attrs.forEach( attr => {
      // attr.name为标签上的属性名称, nodeValue为属性值
      let matched = attr.name.match(/^bind:([-_a-z0-9$]+)(\.\w+)?/i)
      if(matched && matched.length) {
        let eventName = matched[1]
        props[eventName] = {
          callback: attr.nodeValue,
          modifier: matched[2] ? matched[2].replace('.', '') : ''
        }
      }
    })
    Object.keys(props).forEach(key => {
      let modifer = props[key].modifier
      let callbackName = props[key].callback
      if (modifer === 'once') {
        EventEmit.instance.once(key, typeof this.parentContext[callbackName] === 'function' ? this.parentContext[callbackName] : noop)
      } else {
        EventEmit.instance.on(key, typeof this.parentContext[callbackName] === 'function' ? this.parentContext[callbackName] : noop)
      }
    })
  }

  /**
   * 获取某个组件上下文
   */
  getContext(name) {
    const current = this.components[name]
    if (!current) {
      throw new Error(`没有找到${name}组件`)
    }
    return current.instance
  }

  /**
   * 渲染指定组件
   * @param {String} name 要渲染的组件名
   * @param {Object} data 要传输给组件的数据
   */

   render(name, data = {}) {
     let context = this.getContext(name)
     context.render(data)
   }

   run() {
     for (const cname in this.components) {
       // 如果不是动态组件,马上渲染
       if (!this.components[cname].isDeffer) {
         this.components[cname].instance.render()
       }
     }
   }
}

export default ComponentManager

相关文章

  • 实现一个业务组件管理器

  • 一些话

    为了业务逻辑组件的实现与DAO组件的实现分离,程序应该为每个DAO组件都提供接口,业务逻辑组件面向DAO接口编程,...

  • 用AWT编写用户界面二之布局管理器

    布局管理器 :为了实现跨平台的特效并且获得动态的布局结果,java 将容器内的所有组件安排给一个“布局管理器”负责...

  • 基于Arouter实现的组件化方案说明

    基于Arouter实现的组件化方案说明: 基于Arouter实现的组件化方案说明: 一个项目,随着业务的发展,模块...

  • 拦截器原理

    实现模型: 业务组件 业务处理器 代理 客户端业务组件: 业务处理器 代理: 客户端: todo:执行busine...

  • vuex状态管理器实现组件之间的传递

    vuex状态管理器实现组件之间的传递 组件间的传递除了props,emit等方法就足够用了,不然会显得项目繁琐。

  • 组件化

    组件化 如今实现的方案 业务业务之间沟通 解决业务与业务之间页面跳转,路由,或者业务manger的别名 解决业务与...

  • 在小程序中自定义弹窗组件

    因为业务需要在小程序里加上很多的弹窗,就想写一个组件来实现; 创建组件 新建文件夹component专门放组件, ...

  • 译:unet网络概述(2)

    使用网络管理器 该网络管理器是用于管理多玩家游戏的网络状态的组件。它实际上是完全使用高级API(HLAPI)实现的...

  • 装饰模式

    原始组件抽象类Componentabstract Operation: 执行原始业务逻辑 原始组件实现类 Conc...

网友评论

      本文标题:实现一个业务组件管理器

      本文链接:https://www.haomeiwen.com/subject/vyxpdktx.html