A quick write down of notes about how to delete one post item instead of all of them.
The issue: On a posts template (templates/posts.hbs) a helper will send an action to the posts controller (controller/posts.js) which in the case of a delete button would delete all the posts, not just one post item. So what to do?
Solution Summary: Use itemController=”newControllerName” in the helper statement to point the action to a new item controller where you can define the action of deleting the post by sending an ajax request to the server or, alternatively, send the action to the model which sends the ajax request to delete there.
Solution Steps:
– create post controller: ember g controller post
-in templates/posts.hbs adjust the helper code: {{each item is itemController=”post”}}
Note that button code: <button>{{action “destroy”}} now goes to controllers/post.js
Full Code in posts.hbs:
{{each item is itemController=’post’}}
<h2>{{post.title}}</h2>
{{post.model.title}}
<button>{{action: ‘destroy’}}
{{/each}}
– in controller/post add the following code:
Important Note:Because the helper, {{ }}, which calls the action is the posts template (templates/posts.hbs).
the route that is active for this action is the posts route (routes/posts.js).
Which means that the “send” request goes to the active route which is routes/posts.js.
actions: {
destroy: function() {
this.send(‘destroyPost’, this.get(‘model’));
}
}
– Code in routes/posts:
actions: {
var id = post.objectId; (this is how parse identifies items in its database)
return Ember.$.ajax(“https://www.parse.com/1/classes/Post”) + id, {
type: ‘DELETE’
}
}