2

我有一个用于按键的自定义活页夹。当我调用绑定属性时,它不接受我的事件参数。就我而言,我的属性是一个我想用我自己的事件参数触发的函数。这样的事情可能吗?在下面的示例中,值是未定义的,但我如何将值传递回调用者(viewModel.onKeyPress)?

<div id="body">
    <input data-role="combobox" data-bind="keyPress: onKeyPress" />
    <div id="output"></div>
</div>
<script>
  kendo.data.binders.widget.keyPress = kendo.data.Binder.extend({
    init: function (element, bindings, options) {
        kendo.data.Binder.fn.init.call(this, element, bindings, options);
        var binding = this.bindings.keyPress;
        $(element.input).bind("keypress", function(e) {
            var values = { a: 1, b: 2, c: 3 };
            binding.get(e, values); //DOESN'T WORK!
        });
    },
    refresh: function () { }
  });

  var viewModel = kendo.observable({
    onKeyPress: function (e, values) {
        $("#output").append("<div>keyPress (see console.log)</div>");
        console.log(e);
        console.log(values);
    }
  });

  kendo.bind("#body", viewModel);
</script>

http://jsfiddle.net/jpKNr/58/

4

1 回答 1

2

binding.get() 调用将调用该函数而不传递任何参数。您可以使用以下解决方法:

  kendo.data.binders.widget.keyPress = kendo.data.Binder.extend({
    init: function (element, bindings, options) {
        kendo.data.Binder.fn.init.call(this, element, bindings, options);
        var binding = this.bindings.keyPress;

        var method = binding.source.get(binding.path);

        $(element.input).bind("keypress", function(e) {
            var values = { a: 1, b: 2, c: 3 };
            method.call(binding.source, e, values);
        });
    },
    refresh: function () { }
  });

这是一个现场演示:http: //jsbin.com/eriWOq/1/edit

于 2013-10-11T13:30:19.013 回答