Knockout: Different contexts in click binding

No comments

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.

No comments :

Post a Comment