剑道模板绑定奇怪的行为

时间:2016-07-24 20:54:10

标签: javascript kendo-ui

我正在为我的网络应用程序使用Kendo UI。我的应用程序中有这么奇怪(可能)的行为:

控制台上有四个输出:

nb来电:1

nb电话:2

nb电话:3

nb来电:4

我不明白为什么在绑定期间调用getItems函数4次?



<!DOCTYPE html>
<html>
  <head>
    <link href="http://kendo.cdn.telerik.com/2016.2.714/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
    <link href="http://kendo.cdn.telerik.com/2016.2.714/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
    <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2016.2.714/js/kendo.all.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>
  </head>
  <body>
    <div id='listview' data-bind="source: getItems" data-template="item-template"></div>

    <script id="item-template" type="text/x-kendo-template">
      <span>
        <h2>${name}</h2>
      </span>
    </script>

    <script>
      var items = [
        { name: "Item 1" },
        { name: "Item 2" },
        { name: "Item 3" }
      ];    

      var viewModel = kendo.observable({
        calls:0,
        getItems: function() { 
          this.calls++;
          console.log("nb calls: " + this.calls);
          return items;
        },
      });
  
      kendo.bind(document.body, viewModel);

    </script>
  </body>
</html>
&#13;
&#13;
&#13;

你能帮我找一下原因吗?

提前致谢!

0 个答案:

没有答案