Popup using knockout js

亡梦爱人 提交于 2019-12-04 08:22:24

It can be done without custom binding as well. Example is below

            <div class="modalWindowBackground" data-bind="visible: popupDialog" >
                <div class="modalWindow" data-bind="with:popupDialog">
                    <div class="content">
                        <h2 data-bind="text: title"></h2>
                        <p>
                            <span data-bind="text: message"></span>
                        </p>
                        <div class="buttonSpace">
                            <input type="button" class="closeButton" data-bind="value: closeButtonText, click: $root.hidePopupDialog" />
                        </div>                            
                    </div>
                </div>
            </div>

Viewmodel code:

    self.showAlert = function (title, message, closeButtonText) {
        self.popupDialog({ title: title, message: message, closeButtonText: closeButtonText });
    };
    self.hidePopupDialog = function () {
        self.popupDialog(null);           
    };

  //Code which opens a popup
  self.remove = function () {
        .... some code ...
        if (someCondition) {
          self.showAlert('SomeTitle', 'Message', 'OK');
          return;
        }
        .... some code ...
   };

Create a custom binding, have its open / close function trigger on a observable.

I've done a custom binding for jQuery Dialog that uses this approuch in combination with KO templates.

<div id="dialog" data-bind="dialog: { autoOpen: false, modal: true, title: dialogTitle }, template: { name: 'dialog-template', data: dialogItem, 'if': dialogItem }, openDialog: dialogItem"></div>

You can find my binding here along with some others https://github.com/AndersMalmgren/Knockout.Bindings

Live demo http://jsfiddle.net/H8xWY/102/

https://github.com/One-com/knockout-popupTemplate

That pretty much does what you ask for. It's deeply configurable, and under steady development (we use it in our web applications ourselves).

Disclaimer: I'm a One.com developer. I am also the person who originated the above mentioned lib.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!