2013-03-27 66 views
0

我是淘汰賽的新手。但是我想要做的是在嘗試將其發送回服務器之前清理一個視圖模型。我有幾個ko.observable & ko.computed字段我不想發送回服務器(進度& entryType)在提交給服務器之前清理淘汰VM項目

我有一個按鈕,該按鈕綁定到此事件。我首先根據其id找到vm中的項目。刪除不需要的字段是我有麻煩的地方。

self.addTransaction = function(transaction) { 

      var selected = ko.utils.arrayFirst(self.transactions(), function(currentTransaction) { 
       return currentTransaction.id() == transaction.id(); 
      }); 

      if (selected) { 
        console.log(selected); 

       var items = ko.toJS(selected); 
       var mappedItems = ko.utils.arrayMap(items, function(item) { 
        delete item.progress && item.entryType; 
        return item; 
       }); 
       console.log(JSON.stringify(ko.toJS(mappedItems), null, 2)); 
       //send to server     

      } 
     }; 

任何人都可以指出我我如何能做到這一點正確的方向?

謝謝你的幫助!

回答

1

你應該看看knockout mapping plugin

它允許你指定的地圖時,地圖的使用,你可以告訴它忽略屬性:

var mapping = 
{ 
    'ignore': ["propertyToIgnore", "alsoIgnoreThis"] 
}; 
var jsData = ko.mapping.toJS(viewModel, mapping); 
+0

謝謝保羅。我忽略了這個插件! – 2013-03-27 21:30:59