我试图用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
答案 0 :(得分:1)
您必须将contact
记录实例传递给带有签名deleteContact
的操作deleteContact (contact)
。
<button {{action 'deleteContact' contact}}>X</button></td>