Polymer's paper-dialog not working can't be opened

832 views Asked by At

I am trying to implement a paper-dialog box that will reveal itself when a paper-fab is tapped in the image below: my app's main screen

but I can't get the paper-dialog to open.

I have implemented paper-dialog into my app as following:

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/polymerfire/firebase-query.html">
<link rel="import" href="../bower_components/paper-fab/paper-fab.html">
<link rel="import" href="../bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="../bower_components/polymerfire/firebase-auth.html">
<link rel="import" href="shared-styles.html">

<dom-module id="my-view1">
  <template>
    <style include="shared-styles">
      :host {
        display: block;

        padding: 10px;
      }

      paper-fab{
        position:fixed;
        right:20px;
        bottom:68px;
        --paper-fab-keyboard-focus-background:--accent-color;
      }
    </style>
    <firebase-auth 
      id="auth" 
      user="{{user}}" 
      provider="" 
      status-known="{{statusKnown}}"
      on-error="handleError">
    </firebase-auth>
     <firebase-query
        id="query"
        path="/posts"
        data="{{posts}}">
    </firebase-query>
    <div class="card">
      <h1>Post</h1>

      <ul id="post-list">
        <template is="dom-repeat" items="[[posts]]" as="post">
          <li>
            <p class="content">[[post.body]]</p>
          </li>
        </template>
      </ul>

    </div>
    <paper-fab icon="add" onclick="dialog.open()"></paper-fab>

    <paper-dialog id="dialog">
      <paper-textarea id="post" label="Write your post here..."></paper-textarea>
      <paper-button dialog-dismiss>Cancel</paper-button>
      <paper-button on-tap="post" id="btnPost" raised class="indigo" hidden$="[[!user]]">Post</paper-button>
    </paper-dialog>
  </template>

  <script>
    Polymer({
      is: 'my-view1',
      properties:{
        user:{
          type: Object
        },
        statusKnown:{
           type: Object
        },
        posts: {
          type: Object
        }
      },
      post: function() {
        this.$.query.ref.push({
          "Uid": this.user.uid,
          "body": this.$.post.value
        });
        this.$.post.value = null;
      }
    });
  </script>
</dom-module>

    <paper-fab icon="add" onclick="dialog.open()"></paper-fab>

    <paper-dialog id="dialog">
      <paper-textarea id="post" label="Write your post here..."></paper-textarea>
      <paper-button dialog-dismiss>Cancel</paper-button>
      <paper-button on-tap="post" id="btnPost" raised class="indigo" hidden$="[[!user]]">Post</paper-button>
    </paper-dialog>

This snippet here are taken from the demo on this page:

https://www.webcomponents.org/element/PolymerElements/paper-dialog/v1.1.0/demo/demo/index.html

but when I actually tap on the paper-fab, I get the following error:

Uncaught ReferenceError: dialog is not defined at HTMLElement.onclick (view3:1) onclick @ view3:1

Does anyone have any idea how I can make the paper-dialog open when the paper-fab is tapped? I suppose I am missing some includes, but I cannot figure out which one.

1

There are 1 answers

1
Kuba Šimonovský On BEST ANSWER

at first, don't use onclick. There are Polymer event attributes like on-click or on-tap. Second, you should call function which will open the selected dialog

Example:

<paper-fab icon="add" on-tap="_openDialog"></paper-fab>

and inside script

_openDialog: function() {
  this.$.dialog.open();
}

this.$.dialog find element with id dialog and call function open