Emberfire destroyRecord 错误
Emberfire destroyRecord error
当我尝试对商店中的商品调用 destoryRecord
方法时,我目前收到 todo is not defined
。我已尝试通过多种方式重写此代码,但我似乎仍然 运行 遇到问题。
这是我正在处理的文件。它可以很好地发布记录,但我只是 运行 遇到删除它们的问题。
// 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>
removeTodo
函数有问题,传递给find
函数的todo
变量没有在任何地方定义。
removeTodo: function() {
this.store.find('todo', todo /* Where is this coming from */).then(function(todo) {
todo.destroyRecord();
});
}
您需要对模板进行以下更改:
{{action "removeTodo" todo}}
之前的更改使得 each
中可用的 todo
作为 |todo|
传递给操作 removeTodo
而且你需要把removeTodo
函数改成这个
removeTodo: function(todo) {
todo.destroyRecord();
}
现在它接收到迭代上下文中使用的 todo
,您可以在函数中使用它并对其调用 destroyRecord
。
当我尝试对商店中的商品调用 destoryRecord
方法时,我目前收到 todo is not defined
。我已尝试通过多种方式重写此代码,但我似乎仍然 运行 遇到问题。
这是我正在处理的文件。它可以很好地发布记录,但我只是 运行 遇到删除它们的问题。
// 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>
removeTodo
函数有问题,传递给find
函数的todo
变量没有在任何地方定义。
removeTodo: function() {
this.store.find('todo', todo /* Where is this coming from */).then(function(todo) {
todo.destroyRecord();
});
}
您需要对模板进行以下更改:
{{action "removeTodo" todo}}
之前的更改使得 each
中可用的 todo
作为 |todo|
传递给操作 removeTodo
而且你需要把removeTodo
函数改成这个
removeTodo: function(todo) {
todo.destroyRecord();
}
现在它接收到迭代上下文中使用的 todo
,您可以在函数中使用它并对其调用 destroyRecord
。