Cannot create record after ember-data upgrade

拥有回忆 提交于 2019-12-11 18:38:59

问题


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.


回答1:


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","");
}


来源:https://stackoverflow.com/questions/18645636/cannot-create-record-after-ember-data-upgrade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!