0

在最新的 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。任何帮助是极大的赞赏。

4

1 回答 1

7

而不是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","");
}
于 2013-09-05T22:01:02.643 回答