Search code examples
ember.jsember-data

Subscribe to a DS.Model event in controller


I have the following controller :

export default Controller.extend({
  /* model is an array of Posts */

  didDeletePost(post) {
    /* PROBLEM HERE : post is an InternalModel */
    this.get('model').removeObject(post);
    /* do other stuff with post */
  },

  actions: {
    markPostForDelete(post) {
      post.markForDelete(); /* starts a timer */
      post.one('didDelete', this, this.didDeletePost);
    },
    clearMarkPostForDelete(post) {
      post.clearMarkForDelete(); /* cancel a timer which will destroyRecord */
      post.off('didDelete', this, this.didDeletePost);
    }
  }
});

But remove the post from the model in didDeletePost does not work because the post argument is the InternalModel, not the DS.Model.

How can I achieve this ?

As it does not seem to be easy to do it like this, I guess there should be a better way to implement this kind of timer ?


Solution

  • Actually you don't need to remove the post from model.

    Please check out the twiddle I provided : https://ember-twiddle.com/25de9c8d217eafe03aea874f8eefb0fd