簡體   English   中英

同步或異步綁定數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM