I recently struggled with a problem, and although I solved it for me, I’m not sure if there are better solutions there, so I will be grateful for any comments.Problem. I wanted to create a ScrollIntoView binding. Since scrolling an element in a view requires a DOM-Element, I wrote a custom binding, which I would then like to explicitly launch whenever I was glad. I started with this code:
ko.bindingHandlers.scrollTo = {
update: function (element, valueAccessor, allBindings) {
var _value = valueAccessor();
var _valueUnwrapped = ko.unwrap(_value);
if (_valueUnwrapped) {
element.scrollIntoView();
}
}
};
Binding:
<div data-bind="scrollTo: goToThis">
And in the ViewModel, I had this observable:
_self.goToThis = ko.observable(false).extend({notify: 'always'});
which I could just call by calling:
_self.goTohis(true);
. . , goTothis() Observable true, , , . , , if, , if goToThis, . !
, custum :
ko.bindingHandlers.scrollTo = {
update: function (element, valueAccessor, allBindings) {
var _value = valueAccessor();
var _valueUnwrapped = ko.unwrap(_value);
if (_valueUnwrapped) {
element.scrollIntoView();
if (ko.isWriteableObservable(_value) && typeof (_valueUnwrapped) === 'boolean') {
_value(false);
}
}
}
};
, .
, , : - - scrollIntoView? , ?
, ? .. , .
J