Ember Notes on Deleting one post item

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’

}

}

 

Pastor Beth

Rev. Beth Hoskins, aka Pastor Beth is the owner of JuJi Web and an ordained Presbyterian minister in the PC(USA). She is the Stated Supply Minister at Landrum & Inman Presbyterian Churches in the upstate of South Carolina (north of Spartanburg). She has degrees from Clemson University (Chemical Engineering), Columbia Theological Seminary (Masters of Divinity) and The Iron Yard school of computer coding (Front End Development). Currently she resides in Woodruff, SC with her two cat rescues; Joanie Batgirl and Tude the Dude.

Leave a Comment