Knockout: Different contexts in click binding

1 comment

HTML:

<button class="btn btn-success btn-sm" data-bind="click: $root.matchAppointment.bind($parent)">Match appointment</button>

The outlined .bind($parent) above will set this to the parent context of your viewmodel in your javascript:

JavaScript:
self.matchAppointment = function (context) {
      var parent= this;
     ...doStuff(parent);
}

The variable context in function(context) will be the current context where the data-bind occurs (as usual).


Handy for example if you have an Order with Order lines,
and you have some function you want to call from an Order line, but you need also some property from the parent viewmodel Order.

1 comment :

  1. 1xbet korean betting rules - Betway, 1xbet | Legalbet
    1xbet korean betting rules - 1xbet apps Betway, 1xbet | Legalbet

    ReplyDelete