我试图整理一个演示,使用knockout-es5插件来简化使用揭示模块模式的模型.ViewModel1是原始的Knockout模型,它工作正常.ViewModel2尝试使用knockout-es5插件.遇到一些事情
var NS = NS || {};
$(function () {
NS.ViewModel1 = function (first, last) {
var
firstName = ko.observable(first),
lastName = ko.observable(last),
fullName = ko.computed(function () {
return firstName() + " " + lastName();
}),
doSomething = function (n) {
lastName(lastName() + " " + n);
}
;
return {
firstName: firstName,
lastName: lastName,
fullName: fullName,
doSomething: doSomething
};
};
NS.ViewModel2 = function (first, last) {
var
firstName = first,
lastName = last,
fullName1 …Run Code Online (Sandbox Code Playgroud) Ryan N在一些 帖子中使用了一种他称之为"sub-observables"的技术,在那里他将观察者的"父母"观察者挂起.它看起来像这样:
var parent = ko.observable("I'm the parent");
parent.sub = ko.observable("I'm the child");
parent() //="I'm the parent"
parent.sub() //="I'm the child"
Run Code Online (Sandbox Code Playgroud)
这是一种非常方便的技术,我在几个扩展器中使用它.使用Knockout ES5插件,除非你getObservable()在viewmodel上调用get ,否则它看起来无法访问.在绑定中,这看起来很难看,但有时您只是无法访问父对象所附加的对象.
是否存在用于创建和访问子可观察量的ES5兼容方法?
我有一个Knockout扩展,knockout-secure-binding,我们遇到了一个问题.
特别是在使用时Object.defineProperty,如knockout-es5所做的那样,当在一个触发更改事件时,不会调用value绑定的update函数input.
我的单元测试说明了它的特殊性.这有效:
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')
})
Run Code Online (Sandbox Code Playgroud)
这(敲除-s5定义属性的方式)不起作用:
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 …Run Code Online (Sandbox Code Playgroud)