Breeze导航阵列成员是Knockout中的DependentObservables?

时间:2014-10-27 12:19:41

标签: javascript knockout.js breeze

我正在使用很久以前RP Niemeyer发布的clicktoedit绑定处理程序。效果很好:

ko.bindingHandlers.hidden = {
    update: function(element, valueAccessor) {
        ko.bindingHandlers.visible.update(element, function() { return     !ko.utils.unwrapObservable(valueAccessor()); });
    }        
};

ko.bindingHandlers.clickToEdit = {
    init: function(element, valueAccessor) {
        var observable = valueAccessor(),
            link = document.createElement("a"),
            input = document.createElement("input");

        element.appendChild(link);
        element.appendChild(input);

        observable.editing = ko.observable(false);

        ko.applyBindingsToNode(link, {
            text: observable,
            hidden: observable.editing,
            click: observable.editing.bind(null, true)
        });

        ko.applyBindingsToNode(input, {
            value: observable,
            visible: observable.editing,
            hasfocus: observable.editing,
            event: {
                keyup: function(data, event) {
                //if user hits enter, set editing to false, which makes field lose focus
                    if (event.keyCode === 13) {
                       observable.editing(false);
                       return false;
                    }
                //if user hits escape, push the current observable value back to the field, then set editing to false
                    else if (event.keyCode === 27) {
                       observable.valueHasMutated();
                       observable.editing(false);
                       return false;
                    }

                }
            }
        });
    }
};

我遇到的问题是它的转义键不适用于BreezeJS提供的数组中的元素。显然这些是dependentObservables并且没有valueHasMutated方法。我一般都很困惑 a)我如何修改它以适用于Breeze依赖的observables?和 b)为什么它们依赖于可观测量?

任何帮助都将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

Breeze.js已经在关注'撤消'对于您的实体,所以您拥有的绑定处理程序有点不必要。在不确切知道您如何使用它以及可能会影响其他上下文的情况下,您可以随时检查它是否是一个Breeze实体,如果不是,则回退到当前实现 -

keyup: function(data, event) {
//if user hits enter, set editing to false, which makes field lose focus
    if (event.keyCode === 13) {
       observable.editing(false);
       return false;
    }
//if user hits escape, push the current observable value back to the field, then set editing to false
    else if (event.keyCode === 27) {
       if (observable.entityAspect) {
           observable.entityAspect.cancelChanges();
           observable.editing(false);
           return false;
       } else {
           observable.valueHasMutated();
           observable.editing(false);
           return false;
       }
    }

}