2014-01-08 80 views
0

出於某種原因,我無法正確創建observableyarray,以便foreach項目observables更新。此外,我無法獲得更新的模型(observablearay)上的點擊綁定,這是與上述有關。淘汰賽foreach不更新observables

<div>   
    <!-- THIS NEVER UPDATES IF YOU CHANGE TEXTAREA--> 
    <pre data-bind="text: ko.toJSON($data, null, 2)"></pre> 

    <div data-bind="foreach: Items"> 
     <div data-bind="text: TestProperty"></div> 
     <textarea data-bind="text: DataInput"></textarea>   
     <!-- THIS NEVER UPDATES IF YOU CHANGE TEXTAREA--> 
     <span data-bind="text: DataInput"></span> 
    </div> 
    <button data-bind="click: test">Save</button> 
</div> 

JS

var RiskItemModel = function (item) { 
    var self = this; 

    self.DataInput = ko.observable(item.DataInput); 
    self.TestProperty = ko.observable("test"); 
}; 

var PortfolioRiskModel = function (data) { 
    var self = this; 

    self.Items = ko.observableArray(); 
    // fill with initial data 

    if (data) {   
     $.each(data.Items, function (index, item) { 
      self.Items.push(ko.observable(new RiskItemModel(item))); 
      //self.Items.push(new RiskItemModel(item)); 
     }); 
    }; 

    self.test = function() { 
     // I AM ALWATYS GETTING OLD VALUES HERE! 
     alert(ko.unwrap(ko.unwrap(self.Items)[0]).DataInput()); 
    }; 
}; 


var data = { 
    "$id": "1", 
     "Items": [{ 
     "DataInput": "first" 
    } 
        /*, { 
     "DataInput": "second" 
    }*/ 

    ] 
}; 

var ChartSelectedRiskType = new PortfolioRiskModel(data); 
ko.applyBindings(ChartSelectedRiskType); 

你可以用的jsfiddle這裏玩:http://jsfiddle.net/L3tJL/

回答

1

已經綁定你的數據在textarea的錯誤attributte。

應該是這樣的:

<textarea data-bind="value: DataInput"></textarea>