2014-04-02 44 views
0

我在應用程序中有像添加一個新行,刪除一個新行的應用程序。所有的東西都工作正常。就像我第一次加載數據從項目(我有這麼多viewmodel綁定在一個單一的頁面),那裏所有的數據似乎按預期工作(無論何時編輯值,更新我的視圖模型)。但是當我加載第二個時間,視圖模型沒有像預期的那樣更新。即使我編輯了數據,視圖模型也保持着相同的數據。我認爲observable不能正常工作。請謹慎地告訴我在哪裏做錯了。從JSON加載值不更新(如可觀察)敲除

首播時間:

function ViewModel() { 
    var self = this; 

    //Available types - which will come from serverside 
    self.typeDropDown = ko.observableArray(InitialData); 

    self.typeValue = ko.observable(); 

//Explicitly Adding new Row 
    self.Inputs = ko.observableArray([new Item(self.typeDropDown[0], '', '', '')]); 

    self.removeRow = function (Item) { 
     self.Inputs.remove(Item); 
    }, 
    self.addNewRow = function() { 
     //push will add a new element to the container without modifying much in DOM 
     self.Inputs.push(new Item(self.typeDropDown[0], '', '', '')); 
    } 


function Item(Type, StrData, MaxData, Back) { 
    var self = this; 
    self.Type = ko.observable(Type), 
    self.Storage = ko.observable(StrData), 
    self.MaxIOPS = ko.observable(MaxData), 
    self.BackupPercentagePerMonth = ko.observable(Back) 
} 

第二次 - 從服務器加載數據。從服務器

採樣輸入結構:

strJSON = [ 
    { 
     Type: storageInitialData[0].Value, 
     Str: '12', 
     Max: '156', 
     Back: '123' 
    }, 
    { 
     Type: storageInitialData[0].Value, 
     Str: '12', 
     Max: '156', 
     Back: '123' 
    }, { 
     Type: storageInitialData[0].Value, 
     Str: '12', 
     Max: '156', 
     Back: '123' 
    } 
]; 


function ViewModel() { 
    var self = this; 

    //Available types - which will come from serverside 
    self.typeDropDown = ko.observableArray(InitialData); 

    self.typeValue = ko.observable(); 

//Explicitly Adding new Row 
    self.Inputs = ko.observableArray(strJSON); 

    self.removeRow = function (Item) { 
     self.Inputs.remove(Item); 
    }, 
    self.addNewRow = function() { 
     //push will add a new element to the container without modifying much in DOM 
     self.Inputs.push(new Item(self.typeDropDown[0], '', '', '')); 
    } 

是ko.observableArray(strJSON)是不夠的,設置觀察特性在數組中的元素?我是否需要再次調用Item方法?

更新

我的下拉是這樣

<select class="ddText" id="selType" style="width: 100px" 
          data-bind="options:typeDropDown, optionsText: 'Value', optionsValue: 'Value',value : (Type.Value)?Type.Value : Type,attr: { name: 'str',id : 'strType_'+$index()}, uniqueName:true" "> 
         </select> 

回答

0

observableArray項目本身不會自動變成可觀察的。在將其添加到Inputs之前,您必須將對象映射到Itemko.utils.ArrayMap(或者如果你可以確定你在ES5瀏覽器中運行,則內置的map數組方法)在這裏會很有幫助。你可能也想看看ko.mapping插件(官方支持);在你的情況下,它會生成相當於將條目映射到Item s。

+0

謝謝this worked.but,當我重新發送數據textboxe值正確更新,但下拉值不更新。這就是我如何更新下拉值的值:(Hyper.Value)?Hyper.Value:Hyper.am我這樣做是正確的,或者爲了我需要編寫一個自定義的價值的值:selectedvalue。 – codebot

+0

更新您的問題以顯示您現在使用的代碼。 – ebohlman

+0

:我已經更新了這個問題。 – codebot