Search code examples
javascriptember.jsember-data

Cannot create record after ember-data upgrade


After the latest ember-data 1.0 release, I have had some problems creating records. I read in the release notes - https://github.com/emberjs/data/blob/master/TRANSITION.md that:

App.NewPostRoute = Ember.Route.extend({
  model: function() {
    return App.Post.createRecord();
  }
});

is now replaced with:

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

However in my controller I cannot figure it out how to call the createRecord() method, as I have something like this:

addNewTrip:function() {
    var tripDeparature = this.get("tripDeparature");
    var tripArrival = this.get("tripArrival");
    var trips = App.Trips.createRecord({
         tripDeparature: tripDeparature,
         tripArrival:tripArrival,
         isCompleted:false
    });
    trip.save();
    this.set("tripDeparture","");
    this.set("tripArrival","");
}

And it throws an error: ...has no method 'createRecord' (which is expected after the new release), but I cannot figure it out how to call the createRecord correctly. Any help is greatly appreciated.


Solution

  • Instead of App.Trips.createRecord(parameters ...) use this.store.createRecord('trips', parameters ...).

    Your code will become:

    addNewTrip:function() {
        var tripDeparature = this.get("tripDeparature");
        var tripArrival = this.get("tripArrival");
        var trip = this.store.createRecord('trips', {
             tripDeparature: tripDeparature,
             tripArrival:tripArrival,
             isCompleted:false
        });
        trip.save();
        this.set("tripDeparture","");
        this.set("tripArrival","");
    }