javascript - polymer 阵列添加元素两次

标签 javascript arrays polymer

我正在 Polymer 2.0 中制作一个待办事项应用程序。问题是当我添加注释时。它按应有的方式添加它,但是,当我添加另一个时,他会向数组写入 2 个注释。我找不到我的问题。我还使用 var that = this。这不能做得更干净吗?

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/app-layout/app-layout.html">
<link rel="import" href="../bower_components/iron-icons/iron-icons.html">
<link rel="import" href="../bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="../bower_components/paper-button/paper-button.html">
<link rel="import" href="../bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="../bower_components/paper-input/paper-input.html">

<dom-module id="note-app">
<template>
<app-header reveals>
  <app-toolbar>
    <div main-title>Note</div>
    <paper-icon-button icon="delete" on-tap="deleteAllNotes"></paper-
     icon-button>
    <paper-icon-button icon="refresh"></paper-icon-button>
    <paper-icon-button icon="add" on-tap="addNote">+</paper-icon-
    button>
  </app-toolbar>
</app-header>

<paper-dialog id="dialog">
  <h2>Add a new note</h2>
  <paper-input id="title" label="Title of the note"></paper-input>
  <paper-input id="note" label="Content"></paper-input>
  <div class="buttons">
    <paper-button dialog-dismiss>Cancel</paper-button>
    <paper-button dialog-confirm autofocus>Accept</paper-button>
  </div>
</paper-dialog>

<template is="dom-repeat" items={{notes}}>
  <div>#: [[index]]</div>
  <div>Title: [[item.title]]</div>
  <div>Title: [[item.note]]</div>
</template>

class NoteApp extends Polymer.Element {
  static get is() { return 'note-app'; }
  static get properties() {
    return {
      notes: {
        type: Array,
        value: [],
        notify: true,
      }
    }
  }

  ready(){
    super.ready();
  }

  addNote(){
    var that = this;
    this.$.dialog.open();
    this.$.dialog.addEventListener('iron-overlay-closed', function(e){
        if(e.detail.confirmed == true){
          that.push('notes', {title: that.$.title.value, note: that.$.note.value})
          that.$.title.value = "";
          that.$.note.value = "";
        }
    });
  }

  deleteAllNotes(){
    this.splice("notes", 0, this.notes.length)
  }
}

window.customElements.define(NoteApp.is, NoteApp);

最佳答案

非常简单。每次单击 addNote 时,都会创建一个事件。因此,对于每次点击,您都会创建一个新事件,当iron-overlay关闭时,该事件将与所有先前定义的事件一起被调用。这意味着,addEventListener 中的回调被多次调用。

将此代码移动到ready函数中:

   this.$.dialog.addEventListener('iron-overlay-closed', function(e){
        if(e.detail.confirmed == true){
          that.push('notes', {title: that.$.title.value, note: that.$.note.value})
          that.$.title.value = "";
          that.$.note.value = "";
        }
    });

关于javascript - polymer 阵列添加元素两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43497785/

相关文章:

javascript - 如何通过 Jest 测试 Vue 组件的计算 Prop setter

javascript - Foundation CSS 中的选项卡

javascript - 在 this.classList.add() 之后使用 this.updateStyles()

ubuntu - 通过 bower 卡住安装 Polymer

javascript - ShadowRoot 的 getSelection().getRangeAt(0) 在 Google Chrome 35 中返回不正确的 Range 对象

javascript - meteor :服务器和客户端上的不同用户数

javascript - 动态生成的元素触发的事件不起作用

java - 查找整数是否为整数之和之一的有效方法

java - 如何修复 java.lang.ArrayIndexOutOfBoundsException 错误

javascript - 无法理解为什么使用展开运算符