I’m trying to write a todo app with Ember. I’m following some guides, but I’m not doing things exactly as the guide and deletion of records is not working.
This is what I’ve got: router
Router.map(function() {
this.route('home', {'path': '/'});
this.resource('todos', {'path': '/todos' }, function() {
this.resource('todo', {'path': ':/todo_id' })
this.route('new', {'path': '/new'} )
});
});
todo-details component
<li>
{{#if isEditing}}
{{input type="text" class="edit" value=todo.title focus-out="acceptChanges" insert-newline="acceptChanges"}}
{{else}}
{{input type="checkbox" class="toggle" checked=todo.isCompleted}}
<label {{action 'editTodo' on 'doubleClick'}}>{{todo.title}}</label><button class="destroy" {{action 'destroyTodo'}}></button>
{{/if}}
</li>
{{yield}}
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'li',
classNameBindings: ['completed'],
completed: function() {
return this.get('todo.isCompleted');
}.property('todo.isCompleted'),
actions: {
editTodo: function() {
this.set('isEditing', true);
},
acceptChanges: function() {
this.set('isEditing', false);
this.sendAction('edit', this.get('todo'));
},
destroyTodo: function() {
this.sendAction('destroy', this.get('todo'));
}
}
});
todos.index template
{{input type="text" id="new-todo" placeholder="What needs to be done?" value=newValue action="createTodo"}}
<section id="main">
<ul id="todo-list">
{{#each model as |todo|}}
{{todo-item todo=todo edit="editTodo" destroy="destroyTodo"}}
{{/each}}
</ul>
<input type="checkbox" id="toggle-all">
</section>
<footer id="footer">
<span id="todo-count">
<strong>2</strong> todos left
</span>
<ul id="filters">
<li>
<a href="all" class="selected">All</a>
</li>
<li>
<a href="active">Active</a>
</li>
<li>
<a href="completed">Completed</a>
</li>
</ul>
<button id="clear-completed">
Clear completed (1)
</button>
</footer>
todos.index controller
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
createTodo: function() {
this.get('newValue');
var newTodo = this.store.createRecord('todo', {title: this.get('newValue'), isCompleted: false});
this.set('newValue', '');
},
editTodo: function(todo) {
if (Ember.isEmpty(todo.get('title'))) {
this.send('destroyTodo', todo);
}
else {
todo.save();
}
},
destroyTodo: function(todo) {
debugger;
todo.deleteRecord();
}
}
});
The first time I want to delete a todo record, it successfully does it, but it logs to the console Uncaught TypeError: view.destroy is not a function
. From there on, it stops deleting todos.
In case it’s useful: https://github.com/FranGoitia/todo
Thanks.