【发布时间】:2014-03-02 01:21:30
【问题描述】:
我有一个 Knockout 扩展,knockout-secure-binding,我们遇到了an issue。
特别是在使用Object.defineProperty 时,正如knockout-es5 所做的那样,input 上触发更改事件时不会调用value 绑定的update 函数。
我的unit tests 说明了这一特殊性。这有效:
it("reads an input `value` binding", function () {
var input = document.createElement("input"),
evt = new CustomEvent("change"),
context = { vobs: ko.observable() };
input.setAttribute("data-sbind", "value: vobs")
ko.applyBindings(context, input)
input.value = '273-9164'
input.dispatchEvent(evt)
assert.equal(context.vobs(), '273-9164')
})
这(作为淘汰赛-es5 定义属性的方式)不起作用:
it("reads an input `value` binding for a defineProperty", function () {
// see https://github.com/brianmhunt/knockout-secure-binding/issues/23
var input = document.createElement("input"),
evt = new CustomEvent("change"),
obs = ko.observable(),
context = { };
Object.defineProperty(context, 'pobs', {
configurable: true,
enumerable: true,
get: obs,
set: obs
});
input.setAttribute("data-sbind", "value: pobs")
ko.applyBindings(context, input)
input.value = '273-9164'
input.dispatchEvent(evt)
assert.equal(context.pobs, '273-9164')
})
在后一种情况下,如前所述,value.update 不会在调用 input.dispatchEvent 时被调用。
自定义绑定返回它自己的valueAccessor,所以我认为问题与此有关。让我感到特别奇怪的是,它可以与对象属性一起使用,但不适用于 defineProperty。
【问题讨论】:
标签: knockout.js knockout-3.0 knockout-es5-plugin