如何在自定义绑定的bind方法中确定source的当前值

时间:2016-11-02 16:25:40

标签: aurelia aurelia-binding

任何人都知道如何在自定义绑定的bind方法中确定源的当前值?

bind方法定义如下:

bind(binding, scope, [args...])

调用bind时,我想知道绑定中涉及的viewmodel属性(source)的值。

感谢。

1 个答案:

答案 0 :(得分:0)

在自定义绑定行为类中,您可以通过覆盖updateTarget()updateSource()方法来拦截属性的值。例如:

const interceptMethods = ['updateTarget', 'updateSource', 'callSource'];

export class InterceptBindingBehavior {
  bind(binding, scope, interceptor) {
    let i = interceptMethods.length;
    while (i--) {
      let method = interceptMethods[i];
      if (!binding[method]) {
        continue;
      }
      binding[`intercepted-${method}`] = binding[method];
      let update = binding[method].bind(binding);
      binding[method] = interceptor.bind(binding, method, update);
    }

    binding.updateTarget = value => {
      //do something with value;
    }

    binding.updateSource = value => {
      //do something with value;
    }
  }

  unbind(binding, scope) {
    let i = interceptMethods.length;
    while (i--) {
      let method = interceptMethods[i];
      if (!binding[method]) {
        continue;
      }
      binding[method] = binding[`intercepted-${method}`];
      binding[`intercepted-${method}`] = null;
    }
  }
}