次のノックアウト .js マッピング コードがあります。
//JSON string
var startData= {"Name":"SMT","Price":{"Value":22,"Currency":"EUR"}}";
//convert json to model
var viewModel = ko.viewmodel.fromModel(startData);
viewModel.deletePrice= function() {
delete this.Price;
};
ko.applyBindings(viewModel);
次に、Price のネストされたオブジェクトをページに表示する次のテンプレートがあります。
<script type="text/html" id="PriceTemplate">
//render Value and Currency properties from nested object Price
</script>
次に、私のコードで、Price オブジェクトを Teplate PriceTemplate にバインドします。これですべて正常に動作します。
<div data-bind="template: { name: 'PriceTemplate', data: Price, templateOptions: { title: 'Prc'} }"></div>
<a href="#" data-bind="click: function() { deletePrice() }">Delete Price</a>
問題はこの関数deletePrice()
です。呼び出されると、ネストされたオブジェクト Price が削除されますが、テンプレートは初期データを使用してページにレンダリングされたままです。
私の質問 - Price ネストされたオブジェクトを削除し、同時にレンダリングされたテンプレートを削除するにはどうすればよいですか?