当我试图在我的商店中的一个项目上调用destoryRecord方法时,我正在接收destoryRecord。我尝试过用多种方式重写这段代码,但我似乎仍然遇到了一些问题。
这是我正在处理的文件。它发布的记录很好,但我一直遇到删除它们的问题。
// todo/controller.js
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
createTodo: function() {
this.store.createRecord('todo', {
name: this.get('name'),
createdAt: new Date()
});
this.set('name', '');
},
removeTodo: function() {
this.store.find('todo', todo).then(function(todo) {
todo.destroyRecord();
});
}
}
});
// todo/model.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
createdAt: DS.attr('date')
});
// todo/route.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.findAll('todo');
}
});
// todo/template.hbs
{{outlet}}
<div class="jumbotron">
<h2 class="text-center">Add a Todo!</h2>
</div>
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<div class="panel panel-default">
<div class="panel-heading">
<label for="Todo">Add a Todo!</label>
{{input value=name placeholder="Add a Todo"}}
<button class="btn btn-default" {{action "createTodo"}}>Publish</button>
</div>
{{#each model as |todo|}}
<div class="panel-body">
<ul>
<li>
<button class="btn btn-default" {{action "removeTodo"}}>x</button>
{{todo.name}}</li>
</ul>
</div>
{{/each}}
</div>
</div>
</div>发布于 2016-01-05 11:51:24
removeTodo函数有一个问题,传递给find函数的todo变量在任何地方都没有定义。
removeTodo: function() {
this.store.find('todo', todo /* Where is this coming from */).then(function(todo) {
todo.destroyRecord();
});
}您需要对模板进行以下更改:
{{action "removeTodo" todo}}前面的更改使todo (在each中可用)作为|todo|传递给动作removeTodo。
您需要将removeTodo函数更改为
removeTodo: function(todo) {
todo.destroyRecord();
}现在,它接收了迭代上下文中使用的todo,您可以在函数中使用它,并对其调用destroyRecord。
https://stackoverflow.com/questions/34603042
复制相似问题