Difference between model.save() versus model.get('store').commit()

时光毁灭记忆、已成空白 提交于 2019-11-29 18:18:50

问题


What is the difference between

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

and

// '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;
  }, 

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

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

(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())


回答1:


Which one should I use?

I'd recommend using this.get('model').save()

what's the main difference between this.get('store').scheduleSave(this) and this.get('store').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.



来源:https://stackoverflow.com/questions/17309934/difference-between-model-save-versus-model-getstore-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!