javascript - Aurelia 绑定(bind) - 单击按钮返回查看模型

标签 javascript aurelia

在 Aurelia 应用程序中,我有一个带有单个输入的“重命名”表单

<input ... value.bind="something.name" />

和两个按钮:SaveCancel

相同的 something 对象已在其他控件中使用。因此,在单击 Save 按钮之前,我不希望更改 name

是否有一个很好的声明方式来完成它,或者我是否必须将 name 复制到另一个属性,然后在 Save 触发器上将其复制回来?

最佳答案

您可以为您的编辑弹出窗口创建一个模型对象,并且仅在单击保存 时复制对列表中的项目所做的编辑。这是一个简化的示例:https://gist.run/?id=af3af031c5acc4c46407679f5ab1376b

查看

<template>
  <ul>
    <li repeat.for="person of people">${person.firstName} ${person.lastName} <button click.delegate="editPerson(person)">Edit</button></li>
  </ul>
  <div if.bind="editing">
    First Name: <input type="name" value.bind="editModel.firstName" />
    Last Name: <input type="name" value.bind="editModel.lastName" />
    <button click.delegate="savePerson()">Save</button>
    <button click.delegate="cancelEdit()">Cancel</button>
  </div>
</template>

View 模型

export class App {
  editing = false;
  people = [
    { firstName: 'John', lastName: 'Doe' },
    { firstName: 'Jane', lastName: 'Smith' },
    { firstName: 'Bob', lastName: 'Smith' }
  ];

    editPerson(person) {
      this.editing = true;
      this.editObject = person;
      this.editModel = Object.assign({},person);
    }
    savePerson() {
      this.editing = false;

      Object.assign(this.editObject, this.editModel);

      this.editObject = null;
      this.editModel = null;
    }

    cancelEdit() {
      this.personBeingEdited = null;
      this.editing = false;
    }
}

关于javascript - Aurelia 绑定(bind) - 单击按钮返回查看模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37171766/

相关文章:

javascript - '港口' aurelia-skeleton-navigation to typescript

javascript - 无法将 JQuery UI 可拖动助手拖到可放置的子项上

javascript - jQuery DataTable 多行表头

javascript - jQuery 找到 child 的高度并应用于 parent

webpack - 将 css 与 webpack 捆绑在一起

amazon-cognito - 如何让 Amazon Cognito 身份开发工具包在 Aurelia 中工作?

javascript - IE11 中的 JS ForEach 循环

javascript - 更改列宽而不考虑其内容

javascript - Aurelia class.bind 与 checked.bind 问题

Aurelia HttpClient 取消请求