当几个对象共享相同的视图模型时,如何在kendo ui中获取事件源?

时间:2015-01-27 05:37:29

标签: javascript kendo-ui

我有以下最小的repro代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Kendo UI Snippet</title>
    <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css">
    <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css">
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>
</head>
<body>

<div id="popup" >
  <form id="popup-editor-form">
      <div class="k-popup-edit-form k-window-content k-content" style="overflow:visible;">
          <div class="k-edit-form-container">
              <div class="k-edit-field">
                <a href="" data-bind="click:addProducts">Add Additional Products + </a>
              </div>
              <div id="additionalProducts"></div>
          </div>
      </div>
  </form>
</div>


<script>  
  var editWindow = $("#popup").kendoWindow({
      modal: true,
      visible: false,
      resizable: false,
  });

  var viewModel = {};
  viewModel.count = 0;
  viewModel.dropdownChange = function(e) {
    // I need to get the dropdown id that just have been changed here 
    alert('hello');
  }
  viewModel.source = [{Name:"Apples",productValue:"app"},{Name:"Oranges",productValue:"or"}];
  viewModel.addProducts = function() {
    var dropdownMarkup = '<div class="k-edit-label">'+
                    '<label>Product</label>'+
                '</div>'+

                '<div class="k-edit-field">' +
                    '<input name="Product Type' + viewModel.count + '" id="type' + viewModel.count + '"' +
                        'data-role="dropdownlist"' +
                        'data-auto-bind="true"' +
                        'data-value-primitive="true"' +
                        'data-option-label="<No product selected>"' +
                        'data-text-field="Name"' +
                        'data-value-field="productValue"' +
                        'data-text="Select Product..."' +
                        'data-bind="value: productValue'+ viewModel.count + ',' +
                        'source: source,' +
                        'events: { change: dropdownChange}" />' +
                '</div>';
    $('#additionalProducts').append(dropdownMarkup);
    kendo.bind(editWindow, viewModel);
    viewModel.count++;
    return false;
  }

  kendo.bind(editWindow, viewModel);  
  editWindow.data("kendoWindow").title("MyWindow");
  editWindow.data("kendoWindow").center().open();

</script>
</body>
</html>

用户可以添加多个产品。当他们这样做时,会创建一个包含产品选择的新下拉列表。当用户在下拉列表中选择某些内容时会触发dropdownChange事件。

但是我无法弄清楚事件源自哪个下拉列表。 event参数不包含任何有用的内容,this指向viewmodel,对于所有下拉列表都是相同的。

理想情况下,我希望能够接收刚更改的下拉列表的ID - 我需要根据列表中的哪个产品选择此特定下拉列表旁边的其他字段。

如何在'dropdownChange'中找到更改了哪个下拉列表?

1 个答案:

答案 0 :(得分:1)

不确定原始input中需要哪些信息,但e.sender是KendoUI DropDown对象,而e.sender.element[0]input元素。

在以下代码段中,我修改了您的代码以显示input name

$(document).ready(function() {
  var editWindow = $("#popup").kendoWindow({
    modal: true,
    visible: false,
    resizable: false,
  });

  var viewModel = {};
  viewModel.count = 0;
  viewModel.dropdownChange = function(e) {
    // I need to get the dropdown id that just have been changed here 
    alert('hello:' + e.sender.element[0].name);
  }
  viewModel.source = [{Name:"Apples",productValue:"app"},{Name:"Oranges",productValue:"or"}];
  viewModel.addProducts = function() {
    var dropdownMarkup = '<div class="k-edit-label">'+
        '<label>Product</label>'+
        '</div>'+

        '<div class="k-edit-field">' +
        '<input name="Product Type' + viewModel.count + '" id="type' + viewModel.count + '"' +
        'data-role="dropdownlist"' +
        'data-auto-bind="true"' +
        'data-value-primitive="true"' +
        'data-option-label="<No product selected>"' +
        'data-text-field="Name"' +
        'data-value-field="productValue"' +
        'data-text="Select Product..."' +
        'data-bind="value: productValue'+ viewModel.count + ',' +
        'source: source,' +
        'events: { change: dropdownChange}" />' +
        '</div>';
    $('#additionalProducts').append(dropdownMarkup);
    kendo.bind(editWindow, viewModel);
    viewModel.count++;
    return false;
  }

  kendo.bind(editWindow, viewModel);  
  editWindow.data("kendoWindow").title("MyWindow");
  editWindow.data("kendoWindow").center().open();

});
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>

<div id="popup" >
  <form id="popup-editor-form">
    <div class="k-popup-edit-form k-window-content k-content" style="overflow:visible;">
      <div class="k-edit-form-container">
        <div class="k-edit-field">
          <a href="" data-bind="click:addProducts">Add Additional Products + </a>
        </div>
        <div id="additionalProducts"></div>
      </div>
    </div>
  </form>
</div>