Search code examples
data-bindingknockout.jscounter

how to identify data bind in to a span with knockoutjs


    <span class="counter qty empty"
          data-bind="css: { empty: !!getCartParam('summary_count') == false }, blockLoader: isLoading">
         <span class="counter-number">
         <!-- ko if: getCartParam('summary_count') --><!-- ko text: getCartParam('summary_count') --><!-- /ko --><!-- /ko -->
         <!-- ko ifnot: getCartParam('summary_count') --><!-- /ko -->
         </span>
         <span class="counter-label">
            <!-- ko i18n: 'items' --><!-- /ko -->
        </span>
    </span>

the span counter bind some data via KnockoutJS! , i need to know the before and after data bind states in the span ??


Solution

  • There is a beforeChange event that you can subscribe to in your view model:

    myViewModel.counterObservable.subscribe(function(oldValue) {
        alert("The counters's previous value is " + oldValue);
    }, null, "beforeChange");