本文介绍了在ember-data升级后无法创建记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的ember-data 1.0发行版之后,创建记录时遇到了一些问题。我在发行说明中阅读 - :

  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('tripps',参数...)



你的代码将成为:

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


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.

解决方案

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

这篇关于在ember-data升级后无法创建记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:54