简体   繁体   English

Ember-data:如何在控制器操作之间共享和更新事务中的对象?

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

I've found on GitHub a good working example for ember-data under https://github.com/dgeb/ember_data_example and try to extend it by nested resource ('has_many: comments'). 我在GitHub上找到了https://github.com/dgeb/ember_data_example下的ember-data的一个很好的工作示例,并尝试通过嵌套资源('has_many:comments')扩展它。 In the original example a new transaction is created every time the edit view is on and it is submitted/rolled back if the edit mode is leaved. 在原始示例中,每次打开编辑视图时都会创建一个新事务,如果编辑模式处于离开状态,则会提交/回滚该事务。

I wand to add a new comment into content.comments I can't do it and have the error because the 'content' is already in transaction (Error: assertion failed: Once a record has changed, you cannot move it into a different transaction). 我想在content.comments中添加一条新评论我无法做到并且因为“内容”已经在事务中而出现错误(错误:断言失败:一旦记录发生变化,您就无法将其移动到另一个事务中)。

Is the idea I try to realize wrong and I must take another way? 这个想法我试图意识到错了,我必须采取另一种方式吗?

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

I think you could take inspiration from what I did: https://github.com/sly7-7/ember_data_example/commit/57ee7ea6ca44e3a2fbba96fff4ad088a8d786a3c 我想你可以从我做的事情中获取灵感: https//github.com/sly7-7/ember_data_example/commit/57ee7ea6ca44e3a2fbba96fff4ad088a8d786a3c

Perhaps simply doing this.get('content.comments').createRecord({body: ''}) will work. 也许只是这样做this.get('content.comments').createRecord({body: ''})将起作用。 This call refers to the ManyArray.createRecord(), and use the transaction of the owner of the relationship to create the new record. 此调用引用ManyArray.createRecord(),并使用关系所有者的事务来创建新记录。 see https://github.com/sly7-7/data/blob/master/packages/ember-data/lib/system/record_arrays/many_array.js#L163 请参阅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.

相关问题 如何多次更新Ember Data对象? - How to update Ember Data object multiple times? 如何在多线程之间共享一个事务 - how to share one transaction between multi threads 是否可以在.Net应用程序和COM +对象之间共享事务? - Is it possible to share a transaction between a .Net application and a COM+ object? firestore transaction.set(ref, data, {merge: true}) 和 transaction.update(ref, data) 有什么区别? - what difference between firestore transaction.set(ref, data, {merge: true}) and transaction.update(ref, data)? 如何在保持优先级的同时使用.transaction更新数据? - How to update data with .transaction while maintaining priority? 灰烬数据-重新提交当前交易 - Ember Data - re-commit current transaction 如何选择与特定对等方共享交易? - How to choose with a specific peer to share the transaction with? 事务中是否必须包含“选择…锁定共享模式”和“选择…进行更新”? - Do “SELECT … LOCK IN SHARE MODE” and “SELECT … FOR UPDATE” have to be inside of a transaction? UPDATE .. WHERE…-交易可以在这之间发生吗 - UPDATE.. WHERE… - can a transaction happen in between 如何在两个dbContext中与EF6共享一个事务? - how to share a transaction in two dbContext with EF6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM