WEEX H5 Render 解讀(12)之vdom續(xù)3

這節(jié)我們閱讀vdom中有關樣式和事件的代碼。


Element.prototype.setAttr = function (key, value, silent) {
  if (this.attr[key] === value) {
    return
  }
  this.attr[key] = value
  if (!silent && this.docId) {
    const listener = instanceMap[this.docId].listener
    listener.setAttr(this.ref, key, value)
  }
}

Element.prototype.setStyle = function (key, value, silent) {
  if (this.style[key] === value) {
    return
  }
  this.style[key] = value
  if (!silent && this.docId) {
    const listener = instanceMap[this.docId].listener
    listener.setStyle(this.ref, key, value)
  }
}

Element.prototype.setClassStyle = function (classStyle) {
  this.classStyle = classStyle
  if (this.docId) {
    const listener = instanceMap[this.docId].listener
    listener.setStyles(this.ref, this.toStyle())
  }
}

代碼并沒有什么難懂的,我們需要注意的是,對于屬性和樣式,并沒有刪除的函數(shù)。當我們需要刪除某些樣式時,只需把這些樣式的值設為初始值就可以了。

Element.prototype.addEvent = function (type, handler) {
  if (!this.event[type]) {
    this.event[type] = handler
    if (this.docId) {
      const listener = instanceMap[this.docId].listener
      listener.addEvent(this.ref, type)
    }
  }
}

Element.prototype.removeEvent = function (type) {
  if (this.event[type]) {
    delete this.event[type]
    if (this.docId) {
      const listener = instanceMap[this.docId].listener
      listener.removeEvent(this.ref, type)
    }
  }
}

Element.prototype.fireEvent = function (type, e) {
  const handler = this.event[type]
  if (handler) {
    return handler.call(this, e)
  }
}

這里面的handler直接對應于原生的函數(shù)。對于h5來說,對應于同一目錄下的listener.js.



下次將分析vdom原理

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容