使用 knockoutjs 时如何访问滑块的更改事件。我已经为滑块创建了一个自定义绑定,例如从 这里开始 。例如,这就是我想做的:
<div data-bind="slider: minMax, sliderOptions: {min: 20, max: 500, range: 'min', step: 50, values: [20 , 50 ]},sliderChange:DoSomethingWithNewValues" />
我想要一个像sliderChange这样的绑定,它只在滑块更改事件发生时发生,并且想要调用viewModel方法DoSomethingWithNewValues。
function myViewModel() {
var self = this;
self.minMax = ko.observable([20, 50]);
self.DoSomethingWithNewValues = function(){
//make ajax stuff etc with new values
}
}
或者有没有更好的方法来解决这个问题。我不想像这样使用事件处理
$( ".selector" ).slider({
change: function( event, ui ) {
//somehow call myViewModel.DoSomethingWithNewValues ();
//outside of my view model
}
});
我已经试过了
this.minMax.subscribe(function(newminMax) {
//call myViewModel.DoSomethingWithNewValues ();
//this causes it to be called several times if you hover without sliding the slider like
//there is bug or something
}