在ember-data升级后无法创建记录

时间:2013-09-05 20:46:10

标签: javascript ember.js ember-data

在最新的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。非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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","");
}