admin管理员组文章数量:1188829
With the following code, I would expect my update function to execute every time the viewModel.item observable is updated. I can see my init and update functions firing on page load as expected, but not when my button is clicked which updates the observable's value.
Markup:
<button id='addButton'>item++</button>
<br/>
<span>viewModel.item = </span>
<span data-bind='text: $data.item(), bind: viewModel.item'></span>
Script:
$(document).ready(function() {
$('#addButton').click(function() {
viewModel.item(viewModel.item() + 1);
});
var viewModel = {
item: ko.observable(1)
};
ko.bindingHandlers.bind = {
init: function(element, valueAccessor) {
alert('init');
},
update: function(element, valueAccessor) {
alert('update');
}
};
ko.applyBindings(viewModel);
});
I've browsed some of the similar questions and haven't found a comparable example. I've created a JSFiddle here.
With the following code, I would expect my update function to execute every time the viewModel.item observable is updated. I can see my init and update functions firing on page load as expected, but not when my button is clicked which updates the observable's value.
Markup:
<button id='addButton'>item++</button>
<br/>
<span>viewModel.item = </span>
<span data-bind='text: $data.item(), bind: viewModel.item'></span>
Script:
$(document).ready(function() {
$('#addButton').click(function() {
viewModel.item(viewModel.item() + 1);
});
var viewModel = {
item: ko.observable(1)
};
ko.bindingHandlers.bind = {
init: function(element, valueAccessor) {
alert('init');
},
update: function(element, valueAccessor) {
alert('update');
}
};
ko.applyBindings(viewModel);
});
I've browsed some of the similar questions and haven't found a comparable example. I've created a JSFiddle here.
Share Improve this question edited May 26, 2014 at 23:36 Citrus asked May 26, 2014 at 22:56 CitrusCitrus 7662 gold badges13 silver badges27 bronze badges 2 |1 Answer
Reset to default 32The update
function of a binding handler will run whenever the observables it accesses are updated. Your function doesn't access any observables.
Here is an updated version that will work:
ko.bindingHandlers.bind = {
init: function(element, valueAccessor) {
alert('init');
},
update: function(element, valueAccessor) {
ko.unwrap(valueAccessor());
alert('update');
}
};
http://jsfiddle.net/vMD74/14/
本文标签: javascriptKnockout custom binding update not firingStack Overflow
版权声明:本文标题:javascript - Knockout custom binding update not firing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738335663a2076865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
update
on every click of the button? – Robert Krzyzanowski Commented May 26, 2014 at 23:06