任何人都知道如何在自定义绑定的bind
方法中确定源的当前值?
bind
方法定义如下:
bind(binding, scope, [args...])
调用bind
时,我想知道绑定中涉及的viewmodel属性(source)的值。
感谢。
答案 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;
}
}
}