Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61401

How to access events that are data-bound to element in knockout.js

I have date control that looks like this.

<div class="OnTheMoveFormTextBoxContainer">
                       <input data-bind="value: formatDateTime(ko.unwrap(Started)), css: { validationElement: validateItem(Started, 'Started') }, event: {
change: function(d, e) {
    if (Started != $(e)[0].target.value &amp;&amp; dateWheelShown == true) {
        $data.Started(dateToISO($(e)[0].target.value,'DD/MM/YYYY HH:mm'));
    }
}
 }, id: 'Started' + '_' + Id()" data-onthemove_dateformat="DD/MM/YYYY HH:mm" class="Mobile_DateTimePick ui-input-text ui-body-c ui-corner-all ui-shadow-inset ui-mini" data-mini="true" name="Started" id="scoller1400844006695" readonly="">
</div>

How could I access data-bound event handlers in knockout.js, I this current case onchange.

I am looking for something like this: ko.contextFor($($('.OnTheMoveFormTextBoxContainer')[3]).find('input')[0]).events

Upvotes: 0

Views: 78

Answers (1)

G&#244;T&#244;
G&#244;T&#244;

Reputation: 8053

You can use ko.bindingProvider.instance.getBindings.

Something like (untested):

 ko.bindingProvider.instance.getBindings(
              $($('.OnTheMoveFormTextBoxContainer')[3]).find('input').get(0),
              ko.contextFor($($('.OnTheMoveFormTextBoxContainer')[3]).find('input').get(0).$root
 ).event.change

Update Maybe more like: var x = $($('.OnTheMoveFormTextBoxContainer')[3]).find('input').get(0);

ko.bindingProvider.instance.getBindings(x, ko.contextFor(x)).event

See this thread Access knockout binding from child element

Upvotes: 1

Related Questions