带幻影的Ember-CLI给了我这个错误:Uncaught TypeError:无法读取属性' destroyRecord'未定义的

时间:2015-09-13 10:47:29

标签: ember.js ember-cli ember-cli-mirage

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

我的application.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检查器中收到此错误: 未捕获的TypeError:无法读取属性&#39; destroyRecord&#39; of undefineddeleteContact @ application.js:18triggerEvent

1 个答案:

答案 0 :(得分:1)

您必须将contact记录实例传递给带有签名deleteContact的操作deleteContact (contact)

<button {{action 'deleteContact' contact}}>X</button></td>