This is a migrated thread and some comments may be shown as answers.

Passing parameter through click event in data-bind.

3 Answers 2019 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
RodEsp
Top achievements
Rank 2
RodEsp asked on 19 Jun 2012, 06:53 PM
I have something like this:
<div id="view1" data-role="view" data-model="viewModel" data-title="view1">
    <ul id="listview" data-bind="source: dataSource, click: foo" data-role="listview" data-template="template"></ul>
</div>
  
<script type="text/x-kendo-template" id="template">
    <a>${link}</a>
</script>

And on a separate js file:
var viewModel = kendo.observable ({
    foo: function (parameter) {
        //use parameter
    }
});

I want to know if there is any way to pass a parameter to foo through the click event in bind-data.

3 Answers, 1 is accepted

Sort by
0
RodEsp
Top achievements
Rank 2
answered on 19 Jun 2012, 10:35 PM
Nevermind. I solved this issue in another manner.
0
Saravanakkumar
Top achievements
Rank 1
answered on 22 Jun 2013, 06:21 PM
Can you please let me know how did you solve it? I am running into the same issue.
0
leniency
Top achievements
Rank 1
answered on 31 Jul 2013, 03:42 PM
Agreed, would like to see how the OP solved this.

One possibility is that click bindings do pass in the event, with a link back to the target.
<button data-bind="click: submit" data-parameter="foo">Submit</button>
  
submit: function (e) {
  var param = $(e.target).data('parameter');
}
Other bindings though don't send parameters though, or just the model. In calculated fields, the target element isn't available, at least that I've been able to find, and no parameters get passed. You can work around this with a custom binding.
For example, here's one that will watch a collection on the model and trigger if it contains a particular element.
<div data-bind="contains: requiredFields" data-value="Foo">...</div>
<div data-bind="contains: requiredFields" data-value="Bar">...</div>

// Checks that the bound collection contains a value
// specified in the element's data-value attribute.
 kendo.data.binders.contains = kendo.data.Binder.extend({
  refresh: function () {
    var values = this.bindings['contains'].get() || [];
    var value = $(this.element).data('value');
             
    // Only if both there's a collection and search set.
    if (values && value) {
 
      // If the value exists, show the element. Otherwise hide it.
      if ($.inArray(value, values) != -1) {
        $(this.element).show();
      } else {
        $(this.element).hide();
      }
    }
  }
});
 
// Simple model
model = kendo.observable({
  requiredFields: []
});
Tags
MVVM
Asked by
RodEsp
Top achievements
Rank 2
Answers by
RodEsp
Top achievements
Rank 2
Saravanakkumar
Top achievements
Rank 1
leniency
Top achievements
Rank 1
Share this question
or