javascript - 在 Kendo 的自定义 Binder 中添加事件参数?

标签 javascript jquery kendo-ui kendo-mvvm

我有一个用于按键的自定义 Binder 。当我调用绑定(bind)属性时,它不接受我的事件参数。就我而言,我的属性是一个我想用我自己的事件参数触发的函数。这样的事情可能吗?在下面的示例中,值未定义,但如何将值传递回调用者 (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/

最佳答案

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

关于javascript - 在 Kendo 的自定义 Binder 中添加事件参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19310161/

相关文章:

vue.js - vue中设置子组件的props

javascript - Firestore - 听特定的字段变化?

javascript - Angular 中 2 个导入之间的差异

javascript - 为什么我的导入在 Chrome 67 的 EMCAScript 6 中不起作用?

javascript - Backbone this.el 与 this.$el

javascript - 少数元素具有相同的功能

javascript - jQuery 从选项的输出字符串中选择 HTML 选项

javascript - 如何重新排列 KendoGrid Tab 顺序?

javascript - 填充 MeteorJS Autoform 字段

charts - Kendo Chart 控件不会在 IE 10 中呈现正确的 SVG