在Knockout.js中设置observable的可观察属性

时间:2013-09-05 21:50:26

标签: javascript knockout.js knockout-2.0 observable

我在尝试使用Knockout设置observable的可观察属性时遇到了麻烦。 有错误的行已被错误评论。我做错了什么,如何设置该值?

function Event() {
    "use strict";
    var self = this;
    self.timelineId = ko.observable(); 
}

function TimelineViewModel() {
    "use strict";
    var self = this;

    self.editedEvent = ko.observable(new Event());
}

$(document).ready(function () {
    var timelineViewModel = new TimelineViewModel();
    ko.applyBindings(timelineViewModel);

    timelineViewModel.editedEvent.timelineId(0); //Error: TypeError: timelineViewModel.editedEvent.timelineId is not a function
});

1 个答案:

答案 0 :(得分:1)

首先尝试调用editedEvent observable:

timelineViewModel.editedEvent().timelineId(0);

我最初在我的测试用例中遇到了同样的错误,但是这个改变对我有用了!