【问题标题】:Add event args in custom binder for Kendo?在 Kendo 的自定义活页夹中添加事件参数?
【发布时间】:2013-10-11 04:16:25
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery kendo-ui kendo-mvvm


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多