Is providing a mixed type object to a custom binding handlers discouraged?

35 views
Skip to first unread message

Lau Yu Hei

unread,
Sep 28, 2016, 12:05:04 PM9/28/16
to KnockoutJS
Sorry if the title is confusing. 

I study some of the code examples in Knockout documentation and found that usually properties related to the same binding handler are separated, for example in the Custom Binding example
<div data-bind="slideVisible: giftWrap, slideDuration:600">

When the handler of the custom binding slideVisible` is triggered, it will access the binding `slideDuration`, which controls the duration of the slide. However, I am curious why don't instead have syntax like
<div data-bind="slide: {visible: giftWrap, duration:600}">

In this way, it is straightforward to see that the `visible` and `duration` are related to `slide` animation, and moreover we will not "pollute" the data-bind by introducing many data binding names (`slideDuration`, `slideXXX`) for one single binding handler, not to mention user does not need to type the long name `slideDuration`, `slideComplete`, `slide...` for every property.

I do understand that in the world of programming there is not a definite way of implementing things. But I also want to know in the point of view of Knockout developers, is there any particular reason this is acceptable or discouraged? Thank you.

Cheers
Message has been deleted
Message has been deleted

Lau Yu Hei

unread,
Sep 28, 2016, 12:50:17 PM9/28/16
to KnockoutJS
The object literal `{}` is used in some of the data bindings, but is for attributes that are not directly related to each other. For example the `style`, `css` binding handlers accept an object of different HTML styles and CSS classes and their toggle condition. But compared with the example I have given in the original post it is quite different.

Gunnar Liljas

unread,
Sep 28, 2016, 2:41:03 PM9/28/16
to knock...@googlegroups.com
It's mostly a matter of taste. However, for bindings like "options", I would say that the syntax:

options: myrows, optionsText:'name'

is chosen over something like

options: {items:myrows, text:'name'}

because it allows the simplest implementation to be just

options: myrows

and the "option handlers" can be reused for other bindings, like "selectedOptions"

/G


--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages