在数组ko中订阅vm的属性

Che*_*rot 0 knockout.js

我是新手淘汰js并得到一个可观察的数组,当填充数组时,我订阅了我正在添加的每个实例的属性,当属性发生变化时(由于用户交互)我需要知道哪个对象发生了变化,但是淘汰只给我相关财产的新价值.是否有可能得到这个对象?(我在函数上下文中尝试了"this"但没有成功)

  length.isSelected.subscribe(function (isSelected) {
                if (isSelected) { // no access to actual object only the isSelected value
                    debugger;
                    spotLenghts.push(this);
                } else {
                    spotLenghts.pop(this);
                }
            });
Run Code Online (Sandbox Code Playgroud)

RP *_*yer 6

subscribe函数采用第二个参数target.它将控制this执行函数时将设置的内容.

因此,您可以传递length(或者您正在创建的更高级别的对象)作为第二个参数,并且能够this在您的处理程序中使用.