如何使用Ember数据删除新创建的模型?

时间:2022-07-13 15:10:19

I'm implementing a view for creating a new user.

我正在实现创建新用户的视图。

App.UserNewRoute = Ember.Route.extend({
   model : function(params) {
        return this.store.createRecord('user');
   }
});

On the UserNewController there is a method which is triggered if the user presses cancel.

在UserNewController上有一个方法,如果用户按下取消,则触发该方法。

App.UserNewController = Ember.ObjectController.extend({
   //... code
   actions: {
       //.. other actions
       cancel: function(){
            var model = this.get('model');
            model.destroyRecord();
            this.transitionToRoute('users');
       }
   }
});

I'm always getting the following error:

我总是收到以下错误:

Uncaught Error: Attempted to handle event `willCommit` on <App.User:ember751:null> while in state root.deleted.saved. 

I've tried using an alternative:

我尝试过使用替代方案:

  model.deleteRecord();
  model.save(); 

I get the same error.

我犯了同样的错误。

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


1  

The issue might have to do with the fact that model.destroyRecord() returns a promise and that you are transitioning before that promise fulfilled. I have been using the following:

问题可能与model.destroyRecord()返回一个promise以及您在该promise之前完成转换这一事实有关。我一直在使用以下内容:

var model = this.get('model');
var _this = this;

// assuming you are working with a new model and not on say an edit page
// this will delete the new record and once the promise returns will transition routes
if (model.get('isNew')) {
  model.destroyRecord().then(function() {
    _this.transitionToRoute('route name');
  }
}

So that the transition only happens once the promise has been fulfilled. I am still an ember scrub but I figured it wouldn't hurt.

因此只有在履行承诺后才会发生转变。我仍然是一个灰烬磨砂膏,但我认为它不会伤害。

#2


0  

According to this https://github.com/emberjs/data/issues/1669, the issue might be already fixed in newer Ember versions. I'm using Ember version 1.5.1 and Ember-Data version 1.0.0-beta.6

根据这个https://github.com/emberjs/data/issues/1669,问题可能已经在较新的Ember版本中得到修复。我正在使用Ember版本1.5.1和Ember-Data版本1.0.0-beta.6

The workaround was to do a dirty check.

解决方法是进行脏检查。

var model = this.get('model');
model.deleteRecord();
if (model.get('isDirty')) {
    model.save();
}
this.transitionToRoute('users');

#1


1  

The issue might have to do with the fact that model.destroyRecord() returns a promise and that you are transitioning before that promise fulfilled. I have been using the following:

问题可能与model.destroyRecord()返回一个promise以及您在该promise之前完成转换这一事实有关。我一直在使用以下内容:

var model = this.get('model');
var _this = this;

// assuming you are working with a new model and not on say an edit page
// this will delete the new record and once the promise returns will transition routes
if (model.get('isNew')) {
  model.destroyRecord().then(function() {
    _this.transitionToRoute('route name');
  }
}

So that the transition only happens once the promise has been fulfilled. I am still an ember scrub but I figured it wouldn't hurt.

因此只有在履行承诺后才会发生转变。我仍然是一个灰烬磨砂膏,但我认为它不会伤害。

#2


0  

According to this https://github.com/emberjs/data/issues/1669, the issue might be already fixed in newer Ember versions. I'm using Ember version 1.5.1 and Ember-Data version 1.0.0-beta.6

根据这个https://github.com/emberjs/data/issues/1669,问题可能已经在较新的Ember版本中得到修复。我正在使用Ember版本1.5.1和Ember-Data版本1.0.0-beta.6

The workaround was to do a dirty check.

解决方法是进行脏检查。

var model = this.get('model');
model.deleteRecord();
if (model.get('isDirty')) {
    model.save();
}
this.transitionToRoute('users');