store.CreateRecord 推送记录以存储来自服务器的负面响应

store.CreateRecord push record to store even negative response from server

我有以下功能:

    this.store.createRecord('user', params).save().then(()=>{
      this.set('responseModal', 'Utworzono użytkownika!');
    })

我的问题是:为什么 Ember 在服务器响应之前将记录推送到存储? 例如:我填写的表格不正确,我的服务器 return 有错误和状态:400,但我的没有它的 id-key 的记录仍在存储中。我试过这样的事情:

.catch(()=>{
      user.unloadRecord();
    });

但它看起来很糟糕,当某些记录显示并立即隐藏时。是否只有在没有外部库的情况下服务器响应后才能在存储中创建记录?我只想使用 ember-data 将客户端与服务器连接起来。任何意见和建议将不胜感激

如果表单提交不成功,我们将再次尝试使用用户已提供的数据。所以我们需要将数据存储在store中。如果您正在导航到其他页面或不想再次尝试使用旧数据,那么您始终可以使用 rollbackAttributes

If the model hasDirtyAttributes this function will discard any unsaved changes. If the model isNew it will be removed from the store.

尝试在商店中调用 unloadRecord 并传递未正确保存的对象。

.catch(()=>{
  user.unloadRecord();
});

我认为这应该可行:

  //unloads the record from the store if save() fails.
  //This prevents any invalid records from being pushed into the store and viewable to user.
.catch(()=>{
  this.get('store').unloadRecord( {userObjectWithNullId} );
});

另一种方法是使用表单验证。在表单正确之前,用户将无法提交表单。这是docs