首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何侦听polymer.dart中从父级传递到子级的事件

如何侦听polymer.dart中从父级传递到子级的事件
EN

Stack Overflow用户
提问于 2015-12-28 03:52:33
回答 1查看 157关注 0票数 2

我试图传递一个事件从父母到孩子在聚合物1.0.0-rc2在Dart。问题是我不知道该如何去倾听孩子们发生的事情。我尝试过在notation_view.html文件中的dom-模块中放置on-control面板-refire属性,并从那里调用句柄按钮,但是它没有工作。听这件事的正确方式是什么?我知道on-control-panel-button-pressed工作得很好。

notation_view.dart

代码语言:javascript
复制
@PolymerRegister('notation-view')
class NotationView extends PolymerElement {

  NotationView.created() : super.created();

  @reflectable
  handleButton(Event e, var detail) {
    print("received");
  }
}

main_app.dart

代码语言:javascript
复制
@PolymerRegister('main-app')
class MainApp extends PolymerElement {

  MainApp.created() : super.created();

  @reflectable
  refire(Event e, var detail) {
    querySelector('notation-view').dispatchEvent(new CustomEvent("control-panel-refire",
        detail: {'name' : detail["button-id"]}));
  }

main_app.html

代码语言:javascript
复制
<link rel="import" href="notation_view.html">

<dom-module id="main-app">
  <style>
    :host {
      display: block;
    }
  </style>

  <template>
    <notation-view id="notation-view"></notation-view>
    <control-panel id="control-panel" on-control-panel-button-pressed="refire"></control-panel>
  </template>
</dom-module>

notation_view.html

代码语言:javascript
复制
<dom-module id="notation-view" >
    <style>
        :host {
          display: block;
        }
    </style>

    <template>
        <canvas id="canvas" width="1000" height="1000"></canvas>
    </template>

</dom-module>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-28 09:53:59

我还没有试过这个,但我认为

代码语言:javascript
复制
   querySelector('notation-view').dispatchEvent(new CustomEvent("control-panel-refire",
    detail: {'name' : detail["button-id"]})); 
   }

它应该是

代码语言:javascript
复制
   new PolymerDom(root).querySelector('notation-view').fire("control-panel-refire",
    detail: {'name' : detail["button-id"]}));
   }

如果分析器抱怨未知的querySelector()方法,您可以将PolymerElementNotationView的结果转换为fire()

若要侦听事件使用

代码语言:javascript
复制
@PolymerRegister('notation-view')
class NotationView extends PolymerElement {

  NotationView.created() : super.created();

  @reflectable
  handleButton(Event e, var detail) {
    print("received");
  }

  @Listen('control-panel-refire')
  void handleControlPanelRefire(CustomEvent e, [_] /* or `Map detail` instead of [_] */) {
    // handle event
  }

  // alternatively
  EventSubscription _controlPanelRefireSubscr;
  attached() {
    super.attached();
    _controlPanelRefireSubscr = this.on['control-panel-refire'].listen((event) {
      // handle event 
    });
  }

  detached() {
    super.detached();
    _controlPanelRefireSubscr?.cancel();
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34487468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档