javascriptdata-bindingpolymerpolymer-1.0paper-dialog

Polymer Data Bind to a method


I am using a custom element that contains a <paper-dialog>, so that I can reuse the dialog style in my application.

The structure is the following:

Polymer({
    is: 'dialog-confirm',

    properties: {
        title: {
            type: String,
            value: 'Dialog Title',
            reflectsToAttribute: true
        },
        message: {
            type: String,
            value: 'Dialog message',
            reflectsToAttribute: true
        }
    },

    /**
     * Triggered when the document is loaded
     */
    ready: function () {
        var me = this;
    },

    /**
     * Open the dialog
     */
    open: function () {
        this.$.dialog.open();
    }
});

Then, I declare my component as follows in order to have a "prepped" dialog ready to go:

(I removed the <link import="..."> for brevity)

<dom-module id="dialog-confirm">
    <template>
        <style>
        </style>

        <paper-dialog id="confirmation"
                      modal with-backdrop
                      entry-animation="slide-from-top-animation"
                      exit-animation="fade-out-animation"
                      on-iron-overlay-closed="_onSignoutConfirm">
            <h2>{{title}}</h2>
            <paper-dialog-scrollable>
                <p>{{message}}</p>
            </paper-dialog-scrollable>
            <div class="buttons">
                <paper-button class="normal" dialog-dismiss>NO</paper-button>
                <paper-button class="positive" dialog-confirm>YES</paper-button>
            </div>
        </paper-dialog>

    </template>
    <script type="text/javascript" src="dialog-confirm.js"></script>
</dom-module>

The problem is that this is a component, and I would like to expose the iron-overlay-closed event outside the component. Otherwise, when I re-use my component, I can't data-bind this to a new method, as in:

<dialog-confirm id="myDialog" on-iron-overlay-closed="_myCustomMethod"></dialog-confirm>

Is this possible?


Solution

  • The iron-overlay-closed event already bubbles up from the child component, as seen in the following demo. If it doesn't bubble up for you, the problem might be caused by something else not shown in your question.

    <head>
      <base href="https://polygit.org/polymer+1.7.0/components/">
      <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
      <link rel="import" href="polymer/polymer.html">
      <link rel="import" href="paper-dialog/paper-dialog.html">
      <link rel="import" href="paper-button/paper-button.html">
    </head>
    <body>
      <x-foo></x-foo>
    
      <dom-module id="x-foo">
        <template>
          <x-dialog on-iron-overlay-closed="_myCustomMethod"></x-dialog>
        </template>
        <script>
          HTMLImports.whenReady(() => {
            Polymer({
              is: 'x-foo',
              _myCustomMethod: function() {
                console.log('_myCustomMethod: overlay closed');
              }
            });
          });
        </script>
      </dom-module>
      
      <dom-module id="x-dialog">
        <template>
          <paper-dialog opened on-iron-overlay-closed="_onIronOverlayClosed">
            <div class="buttons">
              <paper-button dialog-dismiss>Ok</paper-button>
            </div>
          </paper-dialog>
        </template>
        <script>
          HTMLImports.whenReady(() => {
            Polymer({
              is: 'x-dialog',
              _onIronOverlayClosed: function() {
                console.log('_onIronOverlayClosed: overlay closed');
              }
            });
          });
        </script>
      </dom-module>
    </body>

    codepen