在最新的ember-data 1.0版本发布之后,我在创建记录时遇到了一些问题。我在发行说明中读到https://github.com/emberjs/data/blob/master/TRANSITION.md

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


现在被替换为:

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


但是,在我的控制器中,我无法弄清楚如何调用createRecord()方法,因为我有类似以下内容:

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


而且它抛出一个错误:...没有方法'createRecord'(在新版本发布后会出现),但是我无法弄清楚如何正确调用createRecord。任何帮助是极大的赞赏。

最佳答案

代替App.Trips.createRecord(parameters ...)使用this.store.createRecord('trips', parameters ...)

您的代码将变为:

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

09-18 04:10