Enable condition for click binding

前端 未结 4 1967
慢半拍i
慢半拍i 2020-12-17 00:07

Is there any way to specify an enable condition for the click binding? For example if I have the following:

Clic
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 01:04

    Kyle, after reading your comment to nemsev, I believe you want to disable your button, not your dialog function.

    The binding...

    
    
    Click me for another call to trigger the dialog

    And the code...

    var viewModel = function(){
        var self = this;
        self.allowAlert = ko.observable(true);
        self.allowButtonText = ko.computed(function(){
            if(self.allowAlert()){ 
                return 'Turn off button'; 
            } else { 
                return 'Turn on button'; 
            }
        });
        self.toggleAllowAlert = function(){
            self.allowAlert(!self.allowAlert());
        };
        self.doAlert = function(){
            alert('The dialog has opened');
        };
    }
    
    ko.applyBindings(new viewModel());
    

    http://jsfiddle.net/DQg5P/1/

提交回复
热议问题