model.save() 与 model.get('store').commit() 的区别 [英] Difference between model.save() versus model.get('store').commit()

查看:18
本文介绍了model.save() 与 model.get('store').commit() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

// 'this' is the controller
this.get('model').save();

// 'this' is the controller
this.get('model').get('store').commit();

? 在我进行的小测试中,它们都给了我相同的结果.我应该使用哪个?

? Of the little testing I did, they both gave me the same results. Which one should I use?

我查看了第一个,它调用了

I looked into the first one, and it calls

DS.Model = Ember.Object.extend(
  ...
  save: function() {
    this.get('store').scheduleSave(this);

    var promise = new Ember.RSVP.Promise();

    this.one('didCommit', this, function() {
      promise.resolve(this);
    });

    return promise;
  }, 

那么问题就变成了,this.get('store').scheduleSave(this)this.get('store').commit() 之间的主要区别是什么?

So the question then becomes, what's the main difference between this.get('store').scheduleSave(this) and this.get('store').commit()?

DS.Store = Ember.Object.extend(DS._Mappable, {
  ...
  scheduleSave: function(record) {
    get(this, 'currentTransaction').add(record);
    once(this, 'flushSavedRecords');
  },
  ...
  /**
    This method delegates committing to the store's implicit
    transaction.

    Calling this method is essentially a request to persist
    any changes to records that were not explicitly added to
    a transaction.
  */
  commit: function() {
    get(this, 'defaultTransaction').commit();
  },

我不确定哪个更好.我倾向于 save() 因为它似乎环绕着商店.

I'm not sure which one is better. I'm leaning towards save() because it seems to wrap around the store.

(我在github上没找到这些代码,不知道emberjs的github或amazonaws版本是不是最新的.这里是github上的类似版本:模型的 save() 调用 商店的 scheduleSave()商店的提交())

(I couldn't find these code on github, don't know if the github or the amazonaws version of emberjs is the latest. Here is the similar versions on github: model's save() which calls store's scheduleSave(), and store's commit())

推荐答案

我应该使用哪个?

Which one should I use?

我建议使用 this.get('model').save()

this.get('store').scheduleSave(this)this.get('store').commit() 之间的主要区别是什么?

what's the main difference between this.get('store').scheduleSave(this) and this.get('store').commit()?

如果您在同一个运行循环中保存许多记录,scheduleSave 将批量更改,以便在同一个事务中保存多个记录.在某些情况下,commit 可能会导致对其他记录的更改被持久化.

If you are saving many records during the same run loop, scheduleSave will batch changes so that multiple records will get saved in the same transaction. In some cases commit might cause changes to other records to be persisted.

这篇关于model.save() 与 model.get('store').commit() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆