all files / renders/ Directive.js

100% Statements 21/21
83.33% Branches 10/12
100% Functions 3/3
100% Lines 21/21
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                    463× 463× 463× 463× 463× 224×   463×   463× 450× 44× 44×     406×       463× 1492×   463× 463× 463×   203×   462× 462×    
import { avalon, inBrowser } from '../seed/core'
 
import { Action, protectedMenbers } from '../vmodel/Action'
 
/**
 * 一个directive装饰器
 * @returns {directive}
 */
// DirectiveDecorator(scope, binding, vdom, this)
// Decorator(vm, options, callback)
export function Directive(vm, binding, vdom, render) {
    var type = binding.type
    var decorator = avalon.directives[type]
    Eif (inBrowser) {
        var dom = avalon.vdom(vdom, 'toDOM')
        if (dom.nodeType === 1) {
            dom.removeAttribute(binding.attrName)
        }
        vdom.dom = dom
    }
    var callback = decorator.update ? function (value) {
        if (!render.mount && /css|visible|duplex/.test(type)) {
            render.callbacks.push(function () {
                decorator.update.call(directive, directive.node, value)
            })
        } else {
            decorator.update.call(directive, directive.node, value)
        }
 
    } : avalon.noop
    for (var key in decorator) {
        binding[key] = decorator[key]
    }
    binding.node = vdom
    var directive = new Action(vm, binding, callback)
    if(directive.init){
        //这里可能会重写node, callback, type, name
        directive.init()
    }
    directive.update()
    return directive
}