2
votes

I have an ember/ember-cli application, and am struggling to delete a record. Here is the action definition:

export default Ember.ObjectController.extend({
  actions: {
    remove: function() {
      var model = this.get('model');
      model.destroyRecord();
      this.transitionToRoute('foo');
    }
  }
});

By using console log statements, I can see that this method gets called.

Here is how the action is being called:

<div class="panel-body list-group">
  {{#each tournament in model}}
    {{#link-to 'tournaments.play' tournament class="list-group-item"}}
      <span><strong>{{tournament.name}}</strong> <em>{{tournament.eventDate}}</em></span>
      <span {{action 'remove' tournament}} class="glyphicon glyphicon-trash pull-right"></span>
    {{/link-to}}
  {{/each}}
</div>

I have tried {{action 'remove' tournament}} or {{action 'remove'}} but in either case, the console output is:

Uncaught TypeError: undefined is not a function titlematch-web/controllers/tournaments/index.js:12 __exports__.default.Ember.ObjectController.extend.actions.removevendor.js:43072 Mixin.create.sendvendor.js:33340 runRegisteredActionvendor.js:13360 Backburner.runvendor.js:31397 runvendor.js:33338 handleRegisteredActionvendor.js:51898 (anonymous function)vendor.js:4759 jQuery.event.dispatchvendor.js:4427 elemData.handle

So it seems that my action is called, and some model is present. What am I doing wrong? How can I diagnose this? What I've done seems just like what I see in the guides: http://emberjs.com/guides/models/creating-and-deleting-records/

1

1 Answers

4
votes

You forgot the parameter in your action (unless you want to destroy the controller's model which is the list, not the single tournament.)

destroyRecord returns a promise, so this is better

remove: function(tournament) {
   var controller = this;

   tournament.destroyRecord().then(function() {
       controller.transitionToRoute('foo');
   }, function(error) {
       // todo better error handler, show message to user...
       console.error(error);
   });
}