1

我正在考虑工具箱库中事件冒泡的最佳实践。我有一个带有组件的嵌套标记,并且在此某处按下了一个按钮,该按钮应触发组件层次结构中某处的事件。这是一个例子,我很好奇是否有更好的方法。甚至可能是 toolkithcen 库本身的内置事件系统。

// In one component
mouseClicked: function () {

  var evt = new CustomEvent('ganttChartNewEventRequested');
  document.dispatchEvent(evt);

}

// In another component
document.addEventListener('ganttChartNewEventRequested', function(e){

    alert('create new event');

}, false);
4

1 回答 1

0
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
  <script src='http://toolkitchen.github.io/cdn/toolkit.min.js?shadow'></script>
</head>
<body>
  <my-sink></my-sink>

  <element name="my-source">
    <script>
        Toolkit.register(this, {
          ready: function() {
            setTimeout(function() {
              this.send("hello-world");
            }.bind(this), 500);
            setTimeout(function() {
              this.send("goodbye-world");
            }.bind(this), 1500);
          }
        });
    </script>
  </element>

  <element name="my-sink" on-goodbye-world="goodbyeWorld">
    <template>
        <my-source on-hello-world="helloWorld"></my-source>
        <div >
            {{message}}
        </div>
    </template>
    <script>
        Toolkit.register(this, {
          message: '...',
          helloWorld: function() {
            this.message = 'hello world';
          },
          goodbyeWorld: function() {
            this.message = 'goodbye world';
          }
        });
    </script>
  </element>

</body>
</html>
于 2013-04-19T11:26:49.197 回答