model.save()与model.get('store')之间的区别.commit()

时间:2013-06-26 00:56:36

标签: ember.js ember-model

之间有什么区别
// 'this' is the controller
this.get('model').save();

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

在我做过的小测试中,他们都给了我相同的结果。 我应该使用哪一个?

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

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()之间的主要区别是什么?

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(),因为它似乎环绕着商店。

(我在github上找不到这些代码,不知道github或amazonaws版本的emberjs是最新的。这是github上的类似版本:model's save()调用{{3} }和store's scheduleSave()

1 个答案:

答案 0 :(得分:7)

  

我应该使用哪一个?

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

  

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

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