Jake Robert Powney
Jake Robert Powney

Reputation: 11

How do I delete a post from Firebase using emberfire

I'm new to Ember.js and Firebase and I've managed to add a post and display it onto the screen but I'm stuck on being able to delete it, will anyone be able to help me?

  ////////////////////////////////////////////////////////////
  // CONTROLLER
  ////////////////////////////////////////////////////////////

  App.PostsController = Ember.ArrayController.extend({
  init: function() {
  this.set('post', Ember.Object.create());
  },
  sortProperties: ['timestamp'],
  sortAscending: false, // sorts post by timestamp
  actions: {
  publishPost: function() {
    var newPost = this.store.createRecord('post', {
      title: this.get('post.title'),
      body: this.get('post.body'),
      timestamp: new Date()
      });
      newPost.save();
      },
       removePost: function(post) {
      var post = this.get('post');
      Promise.cast(post.get('post.title')).then(function(posts) {
        posts.removeObject(post);
        post.destroyRecord();
        post.save();
      });
      } 

    }
   });





  ////////////////////////////////////////////////////////////
  // POSTS
  ////////////////////////////////////////////////////////////

  App.PostsRoute = Ember.Route.extend({
  model: function() {
   return this.store.findAll('post');
  }
  });

Upvotes: 0

Views: 621

Answers (1)

Jake Robert Powney
Jake Robert Powney

Reputation: 11

I've managed to fix it the updated code it below:

removePost: function(the_id)
{ 
    console.log("Look, the record class is actually a promise! :D"); 
    console.log(this.get('store').find('post', the_id)); 
    console.log(this.get('store').find('post', the_id).toString());
    this.get('store').find('post', the_id).then(function(rec) {
        rec.destroyRecord();
    });
}

I wasn't passing the id through in the index file, which weren't allowing me to delete the post. A rewrite of the removePost function also helped.

Upvotes: 1

Related Questions