I'm binding my ViewModel's isChecked property to a data-whatever attribute. The value of this attribute is changed by jQuery:
$element.data('whatever', "true")
KnockoutJs doesn't notice this change and my ViewModel's property doesn't change. How can I make KnockoutJs notice it?
I tried something like that, but it didn't work:
JavaScript "view layer" script:
$element.data('whatever', "true")
$element.trigger("myCustomEvent")
The view:
<div data-bind="attr: { 'data-whatever': isChecked, event : {myCustomEvent: checkedFunction} }">
checkedFunction is just an empty method in the ViewModel to fit the event binding syntax. It still looks like KnockoutJs is not listening to myCustomEvent.
EDIT
As Fisherman suggested I used $element.attr instead of $element.data, but it still doesn't work:
<html>
<div id="test" data-bind="attr: { 'data-whatever': isChecked }">my div</div>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://knockoutjs.com/downloads/knockout-3.2.0.js"></script>
<script>
var vm = {
isChecked : ko.observable("one"),
};
$(function () {
ko.applyBindings(vm);
console.log($('#test').attr('data-whatever')); // "one"
$('#test').attr('data-whatever', 'two');
console.log($('#test').attr('data-whatever')); // "two"
console.log(vm.isChecked()); // // "one" again, thouh it should be "two"
});
</script>
</html>
even if I add the event:
<html>
<div id="test" data-bind="attr: { 'data-whatever': isChecked, event :{ myCustomEvent : aFunction }}">my div</div>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://knockoutjs.com/downloads/knockout-3.2.0.js"></script>
<script>
var vm = {
isChecked : ko.observable("one"),
aFunction : function(){ }
};
vm.isChecked.subscribe(function(){
console.log("hit the event"); // never happens
});
$(function () {
ko.applyBindings(vm);
console.log($('#test').attr('data-whatever')); // "one"
$('#test').attr('data-whatever', 'two');
$('#test').trigger('myCustomEvent');
console.log($('#test').attr('data-whatever')); // "two"
console.log(vm.isChecked()); // "one" again, though should be "two"
});
</script>
</html>