簡體   English   中英

Ember-data:如何在控制器操作之間共享和更新事務中的對象?

[英]Ember-data: how to share and update a object in transaction between controller actions?

我在GitHub上找到了https://github.com/dgeb/ember_data_example下的ember-data的一個很好的工作示例,並嘗試通過嵌套資源('has_many:comments')擴展它。 在原始示例中,每次打開編輯視圖時都會創建一個新事務,如果編輯模式處於離開狀態,則會提交/回滾該事務。

我想在content.comments中添加一條新評論我無法做到並且因為“內容”已經在事務中而出現錯誤(錯誤:斷言失敗:一旦記錄發生變化,您就無法將其移動到另一個事務中)。

這個想法我試圖意識到錯了,我必須采取另一種方式嗎?

App.EditContactController = Em.Controller.extend({
  content: null,

  addComment: function () {
    // ERROR here:
    this.get('content.comments').addObject(App.Comment.createRecord({body: ''}));
  },

  enterEditing: function() {
    this.transaction = this.get('store').transaction();
    if (this.get('content.id')) {
      this.transaction.add(this.get('content'));
    } else {
      this.set('content', this.transaction.createRecord(App.Contact, {}));
    }
  },

  exitEditing: function() {
    if (this.transaction) {
      this.transaction.rollback();
      this.transaction = null;
    }
  },

  updateRecord: function() {
    // commit and then clear the transaction (so exitEditing doesn't attempt a rollback)
    this.transaction.commit();
    this.transaction = null;
  }
});

我想你可以從我做的事情中獲取靈感: https//github.com/sly7-7/ember_data_example/commit/57ee7ea6ca44e3a2fbba96fff4ad088a8d786a3c

也許只是這樣做this.get('content.comments').createRecord({body: ''})將起作用。 此調用引用ManyArray.createRecord(),並使用關系所有者的事務來創建新記錄。 請參閱https://github.com/sly7-7/data/blob/master/packages/ember-data/lib/system/record_arrays/many_array.js#L163

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM