淘汰赛""具有可观察性的可观察性

时间:2014-05-06 16:12:29

标签: knockout.js

我正在学习淘汰赛。请任何人帮我解决问题。我想从左侧文本框复制到右侧文本框。在copyValue函数中,我不知道如何访问其他可观察对象,如“self.RightFrontTire”
Javascript模型

var ConditionArray = [{ Id: 1, Title: 'Excellent' },
{ Id: 2, Title: 'Good' },
{ Id: 3, Title: 'Average' }]

var Size = function () {
    var self = this;`enter code here`
    self.First = ko.observable("R");
    self.Second = ko.observable("255");
    self.Third = ko.observable("21");
    self.Last = ko.observable("X");
}
var Tires = function (position) {
    var self = this;
    self.Conditions = ko.observable(ConditionArray);
    self.SelectedCondition = ko.observable("2");
    self.TireSize = ko.observable(new Size());
    self.CopyValue = function (data, event) {
        debugger;
        alert(data)
        switch (event.target.id) {
            case 'btnCopyLeftFrontToRightFront':
                debugger;
                break;
        }
    }
}
var TiresAndWheel = function () {
    var self = this;
    self.LeftFrontTire = ko.observable(new Tires());
    self.RightFrontTire = ko.observable(new Tires());
    self.LeftRearTire = ko.observable(new Tires());
    self.RightRearTire = ko.observable(new Tires());
    self.WheelTypes = ko.observableArray(WTypes);
    self.LeftFrontWheel = ko.observableArray(ConditionArray);
    self.RightFrontWheel = ko.observableArray(ConditionArray);
    self.LeftRearWheel = ko.observableArray(ConditionArray);
    self.RightRearWheel = ko.observableArray(ConditionArray);

}
 var tireAndWheel = new TiresAndWheel();
 ko.applyBindings(tireAndWheel, $('#divTireAndWheel').get(0));

JSFiddle

提前致谢。

1 个答案:

答案 0 :(得分:1)

您的RightFrontTire在不同的视图模型实例中定义。引用该实例来获取其中对象的值。

self.CopyValue = function (data, event) {
    console.log(tireAndWheel.RightFrontTire());
    switch (event.target.id) {
        case 'btnCopyLeftFrontToRightFront':
            break;
    }
}

更新小提琴

http://jsfiddle.net/3DgJz/8/