Can knockout.js wait to bind until an onClick?

2019-09-11 17:01发布

问题:

This is a follow-up question from here: Get observable from array

I now have an observable from a list, and I pass it to a modal for editing like this:

var report = currentViewModel.getReport(reportId);
ko.applyBindings(report,$("#"+targetDiv)[0]);

(targetDiv is the modal that I just loaded via ajax)

Now the modal shows up, and when I edit the report name, as soon as i loose focus, the name is changed in the original list. Im starting to get the hang of this knockout, and that is exactly the behavior that I expect.

My question is, how do I get it to not do that until I click save? There is a cancel button on the modal and I would like for any changes to wait until I click save. I have seen in the documentation that I can do this on a keypress, but I want it on an onclick.

Anyway to do what I am after?

回答1:

Take a look at protected observables.