How to bind Jquery dialog buttons to a knockout viewmodel

前端 未结 2 1639
野的像风
野的像风 2021-02-10 09:56

What I\'d like to do is make a dialog where the buttons are databound to the knockout viewmodel so I can enable or disable those buttons depending on various conditions on the

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-10 10:53

    1. Make sure to apply your own class to the dialog's buttons:

      $("#dialog").dialog({
          buttons: [{
              text: 'Ok',
              class: 'ok-button'
          }]
      });
      
    2. Grab the button.ok-button and apply a data-bind attribute to it (visible here, just to show you that it works). Here, name is an observable property of our view model:

      $("button.ok-button").attr("data-bind", "visible: name().length");
      
    3. Apply bindings normally:

      var model = { name: ko.observable('') };
      ko.applyBindings(model);
      

    Here's an example that hide's an "Ok" button on the dialog if name (an observable) has a length > 0: http://jsfiddle.net/9cRFy/

提交回复
热议问题