I am using knockout.js to display a list of employees. I have a single hidden modal markup on the page. When the "details" button for a single employees is clicked, I want to data-bind that employee to the modal popup. I am using the ko.applyBindings(employee, element) but the problem is when the page loads, it is expecting the modal to start off as bound to something.
So I'm wondering, is there a trick/strategy to do a late/deferred databinding? I looked into virtual bindings but the documentation was not helpful enough.
Thanks!
I would create another observable that wraps the employee.
Attach the click to
showDetails
. Then you can just callapplyBindings
on page load.The JSFiddle linked to in the answer provided by @Romanych didn't seem to work anymore.
So, I built my own example (based upon his original fiddle) with full CRUD support and basic validation using Bootstrap 3 and the Bootstrap Modal library: https://jsfiddle.net/BitWiseGuy/4u5egybp/
Custom Binding Handlers
Example Usage
The View
The ViewModel
Initializing Knockout with the View and the ViewModel
I would like to propose a different way to work with modals in MVVVM. In MVVM, the ViewModel is data for the View, and the View is responsible for the UI. If we examine this proposal:
I strongly agree with
this.detailedEmployee = ko.observable({})
, but I am in strong disagreement with this line:$("#dialog").dialog("show");
. This code is placed in the ViewModel and shows the modal window, wherein fact it is View's responsibility, so we screw-up the MVVM approach. I would say this piece of code will solve your current task but it could cause lots of problems in future.detailedEmployee
toundefined
to have your main ViewModel in a consistent state.As for me, these points are very critical, so I would like to propose a different way. If we "forget" that you need to display data in popup, binding
with
could solve your issue.As you can see, your ViewModel don't know anything about how data should be shown. It knows only about data that should be shown. The
with
binding will display content only whendetailedEmployee
is defined. Next, we should find a binding similar towith
but one that will display content in the popup. Let's give it the namemodal
. Its code is like this:As you can see, it uses the
with
plugin internally, and shows or hide a popup depending on value passed to binding. If it is defined - 'show'. If not - 'hide'. Its usage will be the as with:The only thing you need to do is to use your favorite modals plugin. I prepared an example with the Twitter Bootstrap popup component: http://jsfiddle.net/euvNr/embedded/result/
In this example, custom binding is a bit more powerful; you could subscribe the onBeforeClose event and cancel this event if needed. Hope this helps.