knockout observableArray访问

时间:2013-07-18 13:06:29

标签: javascript knockout.js

如何正确地将OptionsServer发送到服务器?因为我正在尝试将已检查的复选框中的值发送到服务器。

我的代码:

var viewModel = {};

viewModel.ui = {};

viewModel.ui.FlashbackReport = {
    ChoosenEmails : ko.observableArray(),
    ChoosenOptions : ko.observableArray(),
    OptionsServer :  ko.observableArray(),
    Options : ko.observableArray([ "New Contacts", "Updated Contacts","Verified Contacts" ]),
    OptionsToSend: ko.computed({
        read : function () {
            var viewModelShortcut= viewModel.ui.FlashbackReport;
            if (viewModelShortcut.ChoosenOptions()=="New Contacts")
                viewModelShortcut.OptionsServer.push('NEW');  
             else if (viewModelShortcut.ChoosenOptions()=="Updated Contacts")    
                 viewModelShortcut.OptionsServer.push('BETTER'); 
             else if (viewModelShortcut.ChoosenOptions()=="Verified Contacts")  
                 viewModelShortcut.OptionsServer.push('NOT_BETTER');  
        /*   else if (viewModelShortcut.ChoosenOptions()[0]=="New Contacts" 
                     && viewModelShortcut.ChoosenOptions()[1]=="Updated Contacts" 
                     && viewModelShortcut.ChoosenOptions()[2]=="Verified Contacts")
                 viewModelShortcut.OptionsServer.push('NEW','BETTER','NOT_BETTER'); 
        */      
},
    deferEvaluation: true
}),

1 个答案:

答案 0 :(得分:1)

这种方式有效:

OptionsToSend: ko.computed({
    read : function () {
          console.info(viewModel.ui.Report.Options()[0]);
    },
    deferEvaluation: true
})

See fiddle