TruMan1
TruMan1

Reputation: 36158

Add event args in custom binder for Kendo?

I have a custom binder for a keypress. It is not accepting my event args when I call the bound property. In my case, my property is a function that I want to fire with my own event args. Is something like this possible? In the below sample, values is undefined but how do I pass values back to the caller (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/

Upvotes: 2

Views: 1122

Answers (1)

Atanas Korchev
Atanas Korchev

Reputation: 30671

The binding.get() call will invoke the function without passing it any arguments. You can use the following as a workaround:

  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 () { }
  });

Here is a live demo: http://jsbin.com/eriWOq/1/edit

Upvotes: 2

Related Questions