I was wondering if someone had a working example of JQM slider with knockoutjs. I have 2 issues:
1) binding the value to an observable - I can bind it initially i.e. set the "value" of the slider, but this does not update the underlying observable when dragged - I think that this is to do with JQM putting elements over the input to represent it?
2) refreshing the style when changing the min, max, value properties.
Here is a subset of what I have, currentItems changes based on a selection:
workloadViewModel.filterValues = ko.dependentObservable(function () {
var tmp = {};
var itms = this.currentItems();
if (itms.length == 0) return;
tmp.max = itms[0].val;
tmp.min = itms[itms.length - 1].val;
tmp.minRange = this.minRange();
return tmp;
}, workloadViewModel);
ko.bindingHandlers.jqmRefreshSlider = {
update: function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); //just to create a dependency
try {
$("input[type=range]").slider("refresh");
} catch (error) {
trace("error refreshing slider");
}
}
};
<div id="filters" data-bind="template: {name: 'filterTemplate', data: filterValues}, jqmRefreshSlider: filterValues"></div>
<script id='filterTemplate' type='text/html'>
<p>min: ${minRange}</p>
<p>min: ${min}</p>
<p>max: ${max}</p>
<div>
<input type="range" name="slider_min" id="slider_min" value="${minRange}" min="${m开发者_JS百科in}" max="${max}" />
</div>
</script>
Thanks you very much for any help you can give me.
J
This is working binding to silder:
ko.bindingHandlers.slider = {
init: function (element, valueAccessor) {
var val = valueAccessor()();
$(element).slider(
{
value: val,
step: 3,
slide: function (event, ui) {
valueAccessor()(ui.value);
}
});
},
update: function (element, valueAccessor) {
$(element).slider("option", "value", valueAccessor()());
}
};
none of the above worked for me because my slider was being added dynamically
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
</head>
<body>
<span data-bind="text: test"></span>
<br />
<br />
<div class="c-slider" >
<!-- ko if: is -->
<input id="test-i" type="range" min="1" max="10" data-bind="value: test, slider: test" />
<!-- /ko -->
</div>
<input id="test-t" type="text" data-bind="value: test" />
<script type="text/javascript">
function model() {
var self = this;
self.test = ko.observable("1");
self.test.subscribe(function(v) {
alert('s');
$("#test-i").val(v).slider('refresh');
});
self.is = ko.observable(true);
}
ko.bindingHandlers.slider = {
init: function(element, valueAccessor) {
var value = valueAccessor();
$(document).on({
"mouseup touchend": function (elem) {
value($('#' + element.id).val());
}
}, ".c-slider");
}
};
var m = new model();
ko.applyBindings(m);
</script>
</body>
</html>
I was gnashing my teeth for a couple days with the JQM slider. Finally I was able to get true 2-way binding to an observable for the slider's value, and have it update both the thumb slider and the numeric range input. Here is what I came up with:
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);
}
};
And here is how it is used in the HTML:
<input type="range" data-bind="value: currentLineWidth, slider: currentLineWidth" name="penSizeSlider" id="penSizeSlider" min="1" max="150" />
I have updated my Blog with this information as well. More info about integrating Knockout and JQuery Mobile can be found there. http://www.programico.com/1/post/2012/12/knockoutjs-jquerymobile-slider.html
The following worked for me. It also handles dynamically created elements. It's a bit hacky though. I've reused some code from Knockoutjs (version 2.1.0): bind boolean value to select box for autotically handling parsing strings to bools and vice versa:
ko.bindingHandlers.jqmFlip = {
init: function(element, valueAccessor, allBindingsAccessor) {
var observable = valueAccessor(),
interceptor = ko.computed({
read: function() {
return observable().toString();
},
write: function(newValue) {
observable(newValue === "true");
}
});
var result = ko.applyBindingsToNode(element, { value: interceptor });
try {
$(element).slider("refresh");
} catch(x) {
$(element).next('.ui-slider').remove();
$(element).slider();
/*console.log('jqmFlip init error ' + x);*/
}
return result;
},
update: function (element, valueAccessor) {
ko.bindingHandlers.value.update.apply(this, arguments);
var value = valueAccessor();
var valueUnwrapped = ko.utils.unwrapObservable(value);
try {
$(element).slider("refresh");
} catch(x) {
debugger;
console.log('jqmFlip update error ' + x);
}
}
};
精彩评论