all files / directives/duplex/ compact.js

100% Statements 23/23
100% Branches 16/16
100% Functions 5/5
100% Lines 23/23
7 statements, 2 functions, 5 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                        78× 33×       78×     78×         79× 79×       14×   13×                          
 
import { avalon } from '../../seed/core'
import { duplexBeforeInit, duplexInit, duplexDiff, duplexBind, valueHijack, updateView } from './share'
import { updateDataEvents } from './updateDataEvents.compact'
import { updateModel } from './updateDataHandle'
 
 
avalon.directive('duplex', {
    priority: 9999999,
    beforeInit: duplexBeforeInit,
    init: duplexInit,
    diff: duplexDiff,
    update: function (vdom, value) {
        if (!this.dom) {
            duplexBind.call(this, vdom, updateDataEvents)
        }
        //如果不支持input.value的Object.defineProperty的属性支持,
        //需要通过轮询同步, chrome 42及以下版本需要这个hack
        pollValue.call(this, avalon.msie, valueHijack)
        //更新视图
 
        updateView[this.dtype].call(this)
 
    }
})
 
function pollValue(isIE, valueHijack) {
    var dom = this.dom
    if (this.isString
        && valueHijack
        && !isIE
        && !dom.valueHijack) {
        dom.valueHijack = updateModel
        var intervalID = setInterval(function () {
            if (!avalon.contains(avalon.root, dom)) {
                clearInterval(intervalID)
            } else {
                dom.valueHijack({ type: 'poll' })
            }
        }, 30)
        return intervalID
    }
}
avalon.__pollValue = pollValue //export to test
/* istanbul ignore if */
Iif (avalon.msie < 8) {
    var oldUpdate = updateView.updateChecked
    updateView.updateChecked = function (vdom, checked) {
        var dom = vdom.dom
        if (dom) {
            setTimeout(function () {
                oldUpdate(vdom, checked)
                dom.firstCheckedIt = 1
            }, dom.firstCheckedIt ? 31 : 16)
            //IE6,7 checkbox, radio是使用defaultChecked控制选中状态,
            //并且要先设置defaultChecked后设置checked
            //并且必须设置延迟(因为必须插入DOM树才生效)
        }
    }
}