gvas / knockout-jqueryui

Knockout bindings for the jQuery UI widgets.
http://gvas.github.com/knockout-jqueryui/
MIT License
103 stars 38 forks source link

buttonset fires click events when initially created #36

Closed avieru closed 10 years ago

avieru commented 10 years ago

Not sure if it's an issue with knockout-jquery per say, I'm looking for some guidance.

I just want to hook into a click event when one of the button set buttons is clicked, instead the click is fired when the button set is created.

<div id="categories" data-bind="buttonset: { refreshOn: categories }">
    <!-- ko foreach: categories -->
    <input type="radio" data-bind="attr: { id: id }, event: { click: click_event($data) }" name="category"/>
    <label data-bind="attr: { 'for': id },text: display"></label>
    <!-- /ko -->
</div>

Any ideas?

Here is a working fiddle, look in console for output. http://jsfiddle.net/Yj8sG/2/

Thanks

gvas commented 10 years ago

You should use simply click_event instead of click_event($data). Knockout will supply the current item as the first parameter when calling the handler. See Note 1 in the event binding's documentation.

Here is the working jsfiddle:

http://jsfiddle.net/Yj8sG/4/

avieru commented 10 years ago

really appreciate it. so because i was passing $data, it was triggering the event while initially binding the items?

gvas commented 10 years ago

Yes, knockout invoked the handler while parsing the binding string. Essentially ko parses the binding string by eval-ing it. More specifically it creates a Function object around it, here is the related function in the source.