我正在尝试将淘汰赛和 noUiSlider 与自定义绑定融合在一起。我为 jQuery-UI 滑块和 Knockout 找到了类似的代码,并将其用作基础。
如果我使用下面的 sliderko 自定义绑定,则 noUisliders 不会更新 observables。我得到 NaN,输入字段中没有任何内容。
如果我直接使用下面的滑块自定义绑定和 noUiSlider 事件,那么一切正常。滑块自定义绑定表现不佳,可能是由于跟踪 noUiSlider 更新事件。但是,这是我弄清楚如何让滑块不断更新输入字段的唯一方法。
我想使用 Knockout 的 registerEventHandler,但我不确定如何让它工作。
// noUiSlider
ko.bindingHandlers.slider = {
init: function(element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().sliderOptions || {};
noUiSlider.create(element, options);
// works with with noUiSlider but not with knockout event bindings
element.noUiSlider.on('set', function(values, handle) {
var observable = valueAccessor();
observable(values[handle]);
});
// works with with noUiSlider but not with knockout event bindings
element.noUiSlider.on('update', function(values, handle) {
var observable = valueAccessor();
observable(values[handle]);
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
element.noUiSlider.destroy();
});
},
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
element.noUiSlider.set(value);
}
};
// using knockout event handlers
ko.bindingHandlers.sliderko = {
init: function(element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().sliderOptions || {};
noUiSlider.create(element, options);
ko.utils.registerEventHandler(element, 'set', function(values, handle) {
var observable = valueAccessor();
observable(values[handle]);
});
ko.utils.registerEventHandler(element, 'update', function(values, handle) {
var observable = valueAccessor();
observable(values[handle]);
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
element.noUiSlider.destroy();
});
},
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
element.noUiSlider.set(value);
}
};
// initialization for NoUiSlider - saves from adding stuff into page
var sillysv = {
start: [10],
step: 0.01,
range: {
'min': 0,
'max': 100
}
};
var sillysp = {
start: [5],
step: 0.01,
range: {
'min': 0,
'max': 100
}
};
var ViewModel = function() {
var self = this;
self.savings = ko.observable();
self.spent = ko.observable();
self.net = ko.computed(function() {
return self.savings() - self.spent();
});
};
ko.applyBindings(new ViewModel());
<link href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.1.0/nouislider.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.1.0/nouislider.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h2>Slider Demo</h2>
Savings:
<input data-bind="value: savings" />
<div style="margin: 10px" data-bind="slider: savings, sliderOptions: sillysv"></div>
Spent:
<input data-bind="value: spent" />
<div style="margin: 10px" data-bind="slider: spent, sliderOptions: sillysp"></div>
Net: <span data-bind="text: net"></span>