使用Breeze.js取消订阅所有属性更改事件

时间:2013-05-29 19:34:39

标签: javascript knockout.js breeze

关于订阅单个实体的更改事件的文档正在轻松实现 -

    var token;
    var myEntity  = ko.observable();
    if (token == null) {
        token = myEntity().entityAspect.propertyChanged.subscribe(function (changeArgs) { trackChanges(changeArgs); });
    }

但是如果我想订阅observableArray并跟踪令牌

    var tokens = ko.observableArray();
    var myEntitys = ko.observableArray();
    if (tokens().length === 0) {
        ko.utils.arrayForEach(myEntitys(), function (entity) {
            var etoken = entity.entityAspect.propertyChanged.subscribe(function (changeArgs) { trackChanges(changeArgs); });
            tokens.push(etoken);
        });
    }
    console.log(tokens());

订阅工作正常,但每个令牌都等于相同的值

    //This works fine
    myEntity().entityAspect.propertyChanged.unsubscribe(token);

    //**This does not work because myEntitys does not have an entityAspect, of course **
    ko.utils.arrayForEach(tokens(), function (token) {
        myEntitys().entityAspect.propertyChanged.unsubscribe(token);
    });

    //This works, but I am not sure why because token was set above to equal a single entity (myEntity())
    ko.utils.arrayForEach(myEntitys(), function (entity) {
        entity.entityAspect.propertyChanged.unsubscribe(token);
    });

有什么方法可以在离开视图或正确的方法时取消订阅所有内容以执行我上面尝试的操作?如果我再次重新加载相同的视图,则会双重订阅propertyChange事件。

1 个答案:

答案 0 :(得分:0)

我们能够重现这个问题并正在调查这个问题。