Skip to content

Instantly share code, notes, and snippets.

@FirstVertex
Created December 4, 2012 19:06
Show Gist options
  • Save FirstVertex/4207568 to your computer and use it in GitHub Desktop.
Save FirstVertex/4207568 to your computer and use it in GitHub Desktop.
Knockout Binding Handler for JQuery Mobile Slider
ko.bindingHandlers.slider = {
init: function (element, valueAccessor) {
// use setTimeout with 0 to run this after Knockout is done
setTimeout(function () {
// $(element) doesn't work as that has been removed from the DOM
var curSlider = $('#' + element.id);
// helper function that updates the slider and refreshes the thumb location
function setSliderValue(newValue) {
curSlider.val(newValue).slider('refresh');
}
// subscribe to the bound observable and update the slider when it changes
valueAccessor().subscribe(setSliderValue);
// set up the initial value, which of course is NOT stored in curSlider, but the original element :\
setSliderValue($(element).val());
// subscribe to the slider's change event and update the bound observable
curSlider.bind('change', function () {
valueAccessor()(curSlider.val());
});
}, 0);
}
};
<input type="range" data-bind="value: currentLineWidth, slider: currentLineWidth" name="penSizeSlider" id="penSizeSlider" data-theme="a" data-track-theme="c" min="1" max="150" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment