[英]Binding data synchronous or asynchronous
綁定功能在敲門js上同步嗎? 在此示例中,是否總是在getDeliveryNote
函數中獲取數據?
function myModel(){
var self = this;
self.orders = ko.observable();
self.selectAndClick = function(data){
self.orders(data);
self.getDeliveryNote();
}
self.getDeliveryNote(){
console.log(self.orders()); // would i ALWAYS get the data here?
}
}
是的,所有內容都會同步更新。 是的, getDeliveryNote
將始終包含您使用self.orders(data);
設置的self.orders(data);
你可以考慮使用ko.computed
使這里deliveryNote
取決於orders
,那么你就不會需要調用getDeliveryNote
可言。 例如。
self.deliveryNote=ko.computed(function(){
//...using orders() inside this function will cause 'deliveryNote'
//...to be reevaluated when the value of 'orders' changes
//...(ie this function will be called
//...when orders(data) is called with a different 'data' from last time)
return orders().deliveryNote() // for example
});
現在,您可以使用deliveryNote()
,它將始終與訂單中的商品保持同步。
然后,您也不需要selectAndClick
函數,因為您可以直接綁定到orders
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.