1

http://jsfiddle.net/AYPze/9/

In this example I have two similar divs with the same goal. Bind the selected date from the datetimepicker and save it to the object binded by knockout js.

The problem with the first div is that the datetimepicker won't show up because i use the knockout "with" binding.

The second div uses the normal knockout js binding syntax, which works fine with the datetimepicker.

I experienced this behavior also with the jquery-ui Accordion

My Questions: Is this a bug in knockout or jquery-ui? Is there a work around, so I can use the "with" binding?

Henk
  • 750
  • 10
  • 21
  • 1
    possible duplicate of [Knockout with Jquery UI datepicker](http://stackoverflow.com/questions/6612705/knockout-with-jquery-ui-datepicker) see also: https://github.com/SteveSanderson/knockout/issues/239 – fyr Feb 09 '12 at 11:03

1 Answers1

3

Your problem is related to the with binding, but not in the way you think.

The problem you have is that the with binding in this case will remove the jQuery datepicker from the DOM element and that is why you don't see the datepicker for the first textbox.

The main problem here is that you are breaking a very important rule when working with Knockout and the DOM. You shouldn't access the DOM directly with jQuery like you are doing now. You have to use a bindingHandler to bridge the gap between your data model and the DOM model.

The Binding Handlers seem complex at first, but they are pretty handy once you get to know them.

Here is an updated version of your fiddle with a working datepicker: http://jsfiddle.net/AYPze/10/

Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79
  • thanks Mikael for your post. Custom bindings are real awsome. Please lock at my solution in http://jsfiddle.net/AYPze/12/ I use the custom binding "datepicker" to set the datepicker config and still use the "value" binding for the values. – Henk Feb 09 '12 at 17:25
  • Sorry to pick up on such an old thread, but this seems to explain exactly a problem I'm also having. I'm very new to the world of Knockout, and am really just tweaking code written by someone else which employs Knockout. Having reviewed your jsfiddle example (which I don't really follow), I can't wondering if all that extra complication is worth the effort. Using jQuery, I can animate an element with one line of code. That's the beauty of jQuery. Now, with Knockout, it seems that it might take a dozen lines, if I have to use a bindingHandler and so on. – Philip Stratford Feb 17 '16 at 11:17
  • FIrst of all, I think my solution is dated and would not recommend using it. Then, about using jQuery directly will look like it has the desired effect but it hasn't. Not together with Knockout anyway. The reason is that Knockout has a totally different concept on how to deal with data and how that data is bound to the view (And that the data and the presentation is separated). You need to understand that concept and the reasoning behind it in order to understand Knockout. – Mikael Östberg Feb 18 '16 at 09:53
  • That said, if there isn't any really good reason for you to use Knockout, I suggest you look at something else instead. Knockout was one of the first frameworks that offered the concept I'm referring to, but there are better options out there today. Check out http://riotjs.com or maybe https://angularjs.org for better solutions (imho). – Mikael Östberg Feb 18 '16 at 09:55