Exemple #1
0
 /**
  * 清除所有父子引用
  * @returns {void} 无返回
  */
 clearReference() {
   each(this.target, function (name, value) {
     if (isNull(value)) return;
     let child = value[OBSERVER_PROP_NAME];
     if (child) this.removeChild(child);
   }, this);
 }
Exemple #2
0
 /**
  * 添加指令
  * @param {Object} directives 指令集 
  * @returns {void} 无返回
  */
 registerDirectives(directives) {
   each(directives, (name, directive) => {
     name = toSplitCase(name);
     let fullName = directive.meta.prefix === false ?
       name : `${this.prefix}:${name}`;
     if (directive.meta.type == Directive.types.ELEMENT) {
       this.elementDirectives[fullName.toUpperCase()] = directive;
     } else {
       this.attributeDirectives[fullName.toLowerCase()] = directive;
     }
   });
 }
Exemple #3
0
module.exports = function (parent, props) {
  //新的 scope 因为「继承」了 _observer_ 
  //所以在新 scope 上进行双向绑定时,将将值成功回写
  //如果有天不须用 cteate 继承法,需要注意 _observer_ 
  //或在新 scope 上 defineProperty 代理 parentScope
  let scope = create(parent);
  copy(props, scope);
  //将 func 绑定到原 scope 上;
  each(parent, (key, value) => {
    if (!isFunction(value)) return;
    scope[key] = value.bind(parent);
  });
  return scope;
};
Exemple #4
0
 /**
  * 移除「一个/一组/所有」事件监听函数
  * @param {string} name 事件名称
  * @param {function} listener 事件处理函数
  * @param {capture} capture 是否是捕获阶段事件(只在代理 dom 对象时有效)
  * @returns {void} 无返回
  */
 removeListener(name, listener, capture) {
   if (name && listener) {
     if (this._isNative_) {
       this._removeNativeEventListener(name, listener, capture);
     }
     if (!this._listeners_[name]) return;
     let index = this._listeners_[name].indexOf(listener);
     this._listeners_[name].splice(index, 1);
   } else if (name) {
     if (this._isNative_ && this._listeners_[name]) {
       this._listeners_[name].forEach(function (_listener) {
         this.removeListener(name, _listener, capture);
       }, this);
     }
     delete this._listeners_[name];
   } else {
     each(this._listeners_, function (name) {
       this.removeListener(name, null, capture);
     }, this);
     this._listeners_ = {};
   }
 }
Exemple #5
0
 /**
 * 初始化一个编译完成的 handler
 * @param {function} handler 编译后的的模板函数
 * @param {Object} options 选项
 * @returns {void} 无返回
 */
 _bindHandler(handler, options) {
   //排序 directives
   handler.directives = handler.directives.sort(function (a, b) {
     return b.meta.level - a.meta.level;
   });
   //初始化 directives
   let boundDirectives = [];
   each(handler.directives, (index, directive) => {
     directive.index = index;
     directive.bind();
     boundDirectives.push(directive);
     //移除完成绑定的指令对应的 attribute
     if (directive.meta.remove !== false &&
       directive.attribute && options.remove !== false) {
       directive.node.removeAttribute(directive.attribute.name);
     }
     //如果遇到一个「终态」指令,停止向下初始化
     if (directive.meta.final) {
       handler.final = true;
       return handler.final;
     }
   });
   handler.directives = boundDirectives;
 }