3

I'm creating a custom binding that sets up focus and blur behavior in the init method. Otherwise, I'd like this custom binding to operate exactly like the native Knockout value binding.

 ko.bindingHandlers.currencyValue = {

init: function (element, valueAccessor) {
    $(element).focus(function () {
        //focus behavior here...
    });

    $(element).blur(function () {
        //blur behavior here...
    });

   //what do I put here to get the native value binding behavior?
},
update: function (element, valueAccessor) {
    //what do I put here to get the native value binding behavior?
}
4

2 回答 2

2

你应该调用ko.bindingHandlers.value.initko.bindingHandlers.value.update函数:

ko.bindingHandlers.currencyValue = {
init: function (element, valueAccessor, allBindings, viewModel, context) {
    $(element).focus(function () {
        //focus behavior here...
    });

    $(element).blur(function () {
        //blur behavior here...
    });

   return ko.bindingHandlers.value.init(element, valueAccessor, allBindings, viewModel, context);
},
update: function (element, valueAccessor, allBindings, viewModel, context) {
    return ko.bindingHandlers.value.update(element, valueAccessor, allBindings, viewModel, context);
}
于 2013-04-11T15:19:29.033 回答
0

一个比@Artem解决方案更易于维护和可读的解决方案- 以防有一天 KO 将额外的参数传递给initandupdate函数并且需要更少的击键:

ko.bindingHandlers.currencyValue = {
init: function (element, valueAccessor, allBindings, viewModel, context) {
    $(element).focus(function () {
        //focus behavior here...
    });

    $(element).blur(function () {
        //blur behavior here...
    });

   return ko.bindingHandlers.value.init.apply(this, arguments);
},
update: function (element, valueAccessor, allBindings, viewModel, context) {
    return ko.bindingHandlers.value.update.apply(this, arguments);
}
于 2015-09-27T16:57:10.377 回答