Search code examples
ember.jsember-dataember-router

emberjs new-router-V3/controller can't create or edit record using ember-data


In this jsfiddle, I have EmBlog.PostsNewRoute and EmBlog.PostsEditRoute. The routes contain events for 'save, cancel and destroy'.

When I create a new record, it only creates it in memory and never calls the store.commit() and in the console, it throws the error:

Uncaught TypeError: Cannot call method 'commit' of undefined

When I try to edit, it throws thesame error but then the edit still happens only in memory.

The destroy action also fails.

When I call cancel, I get:

Cannot read property 'defaultTransaction' of undefined

Most of the code is in the jsfiddle. The save and cancel events follow the pattern described by Yehuda here:

    App.NewUserRoute = Ember.Route.extend({
      model: function() {
         return App.User.createRecord();
      },

     events: {
         save: function(user) {
          this.get('store').commit();
         }
     }
   });

Thanks


Solution

  • Updated fiddle! it is now working for create, edit and destroy use cases. See below for details on what I changed...

    When I create a new record, it only creates it in memory and never calls the store.commit() and in the console, it throws the error: Uncaught TypeError: Cannot call method 'commit' of undefined

    original PostNewRoute failed because this.store is undefined. Also this.content would have been undefined.

    save: function(post) {
      this.store.commit();
      this.content.addObserver('id', this, 'afterSave');
     },
    

    Updated version calls commit on the post's transaction. Also using post.one callback to transition after record is created.

     save: function(post) {
       post.one('didCreate', this, function(){
         this.transitionTo('posts.show', post);
       });
       post.get('transaction').commit();
     },
    

    Will update later with details on other updates...

    When I try to edit, it throws thesame error but then the edit still happens only in memory.

    The destroy action also fails.

    When I call cancel, I get: Cannot read property 'defaultTransaction' of undefined