0

我正在尝试使用 mirage 模拟服务器创建一个简单的 Ember-CLI 应用程序。我可以在数据库中插入和生成随机数据,但不能删除记录。

我的应用程序.hbs

<h2 id="title">Contacts</h2>

{{#each contact in model}}
<table>
<tr>    
  <td>{{contact.name}}</td>
  <td>{{contact.surname}}</td>
  <td>{{contact.age}}</td>
  <td><button {{action 'deleteContact'}}>X</button></td>  //button to delete the contact
</tr>
</table>
{{/each}}

<br>
<p>New contact</p>
{{input value=newName}}
<button {{action 'createContact' newName}}>Create contact</button>

在 application.js 我定义了动作

import Ember from 'ember';

export default Ember.Route.extend({
    model() 
    {
        return this.store.findAll('contact');
    },

    actions: 
    {
        createContact(newName)
        {
            this.store.createRecord('contact',
            {
                name: newName
            }).save();
        },

        deleteContact(contact) 
        {   
            return contact.destroyRecord(); // if I comment this line the error doesn't show
        }
    }
});

当我单击按钮时,我在 Ember 检查器中收到此错误:Uncaught TypeError: Cannot read property 'destroyRecord' of undefineddeleteContact @ application.js:18triggerEvent

4

1 回答 1

1

您必须使用签名将contact记录实例传递给您的操作。deleteContactdeleteContact (contact)

<button {{action 'deleteContact' contact}}>X</button></td>
于 2015-09-13T11:03:54.277 回答