gpt4 book ai didi

javascript - 在拦截绑定(bind)行为期间,“this”不引用当前对象上下文

转载 作者:行者123 更新时间:2023-12-01 02:00:05 25 4
gpt4 key购买 nike

在 Aurelia 应用程序中,我正在使用绑定(bind)行为。它看起来像这样:

<div id="slider" ej-slider="e-value.two-way:controller.item.progress & intercept:controller.saveChange;" ></div>

拦截绑定(bind)行为from here正在绑定(bind) controller.saveChange它确实被调用了。

但是,问题是在该方法中,this不是指 Controller ,而是指绑定(bind)。因此我无法访问进行实际保存所需的 Controller 的方法和属性。

这就是绑定(bind)行为的样子:

export class InterceptBindingBehavior {

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

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

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

如何解决这个问题?

最佳答案

与我的评论相关,作为一个快速而肮脏的修复,我会尝试分别将类和方法作为参数传递。我测试了它,它确实有效。但也许一些 aurelia 专家知道如何处理这个问题的更好方法:

html

<div id="slider" ej-slider="e-value.two-way:controller.item.progress & intercept:controller:'saveChange'"></div>

InterceptBindingBehavior.ts

export class InterceptBindingBehavior {
readonly interceptMethods = ['updateTarget', 'updateSource', 'callSource'];

bind(binding, scope, interceptorClass, interceptorMethod) {
let i = this.interceptMethods.length;
while (i--) {
let method = this.interceptMethods[i];
if (!binding[method]) {
continue;
}
binding[`intercepted-${method}`] = binding[method];
let update = binding[method].bind(binding);
binding[method] = interceptorClass[interceptorMethod].bind(interceptorClass, method, update);
}
}

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

关于javascript - 在拦截绑定(bind)行为期间,“this”不引用当前对象上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50668958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com