How to show alerts / popups / windows using knockout.js

I think we should not have links to html elements in the viewmodel. So I think I can not do $ ('# id'). Dialog () or should not do a warning ('message') or window.open (). Then how do I do this?

+4
source share
2 answers

Take a look at this answer to a sample of using custom bindings to control the jQuery UI dialog box from your view model.

jquery ui dialog integration with knockoutjs

Setting the observable to true opens a dialog box, setting it to false, closing it.

+8
source

In fact, using dialogs does not represent any effort. Just let Knockout make it mandatory, and the data will be in the dialog elements. In my application, if the dialog is open or not, this does not affect. The knockout will still update the values.

One consideration though, I am doing my bindings in logical partitions. Therefore, I would like ko.applybindings to be applied to the main body of the page and the dialog separately in two calls.

As for warnings, you must pass the text to be displayed. Therefore, to use the knockout here, you must allow the knockout to update the hidden element. Then get the resulting value to display in your alert.

For window.open, not sure what your problem is. As far as I understand, this is the same as moving to a new page. Thus, the logic of the page will fill.

+1
source

Source: https://habr.com/ru/post/1388261/


All Articles