最佳方式复制一个可观察到的在淘汰赛

问题描述:

我有一个敲除模型最佳方式复制一个可观察到的在淘汰赛

self.newItem = ko.observable({ 
     manufacturer: ko.observable(), 
     itemnumber: ko.observable(), 
     itemDescription: ko.observable(), 
     priceclass: ko.observable()  

    }); 

,我有另一个具有相同的性质,但只有itemnumber是可观察的。

self.newItemToInsert = ko.observable({ 
     manufacturer: "", 
     itemnumber: ko.observable(), 
     itemDescription: "", 
     priceclass: "" 

    }); 

我还有一个观察的阵列来存储项目

self.AllItems = ko.observableArray(); 

现在我的问题是我怎么能复制的newitem到newItemToInsert观察到的,所以我可以将它保存到AllItems阵列,并有itemnumber可观测为阵列中的不同行。因此,如果我添加10个项目,我希望能够跟踪每个10个itemnumber属性中的数据更改。

感谢

如果我正确了你的问题,你可以有你的项目一个单独的视图模型并为每个项目将创建模型的new实例。然后在您的物料视图模型中,为所需的任何变量定义observable

例子:https://jsfiddle.net/9aLvd3uw/222/

VM:

var MainViewModel = function() { 
    var _self = this; 
    var i = 1; 
    _self.Items = ko.observableArray([]); 
    //Fake Data 
    _self.Items.push(new ItemViewModel({ "manufacturer": "Co.0", "itemnumber": 123 ,"itemDescription": "Desc 0" , "priceclass" : "Class 0"})); 
    //Add a new item with fake data 
    self.ClickMe = function(){ 
    _self.Items.push(new ItemViewModel({ "manufacturer": "Co."+i, "itemnumber": 123 + i ,"itemDescription": "Desc" +i , "priceclass" : "Class "+i})); 
    i++; 
    } 
} 

var ItemViewModel = function (data) { 
    var _self = this; 
    _self.manufacturer = data.manufacturer; 
    _self.itemnumber = ko.observable(data.itemnumber); 
    _self.itemDescription = data.itemDescription; 
    _self.priceclass = data.priceclass; 
} 
ko.applyBindings(MainViewModel); 
+0

非常酷的。可你也请告诉我怎样才能删除行。 – sp9

+0

查看已更新的'jsfiddle'示例。 –

+0

美丽......非常感谢 – sp9