Where are Ember Data's commitDetails' created/updated/deleted OrderedSets set?

你说的曾经没有我的故事 提交于 2019-12-24 11:44:25

问题


Ember Data's Adapter saves edited records in different groups of Ember.OrderedSets, namely: commitDetails.created, commitDetails.updated, and commitDetails.deleted.

model.save() from model controller's createRecord() will be placed in the commitDetails.created group. model.save() from model controller's acceptChanges will placed be in the commitDetails.updated group. But I can't find in code where the placement association happens.

I know that they are instantiated in Ember Transaction's commit function (which calls Adapter's commit, in turn calling Adapter's save). Throughout this process, I can't figure out where exactly the records are sorted according to the created/updated/deleted criteria.


回答1:


I'm not quite clear what you're asking, but if you're looking for where records get added to their appropriate commitDetails set, I believe this is the line you're looking for, in the commitDetails property itself.

Here's the relevant code.

forEach(records, function(record) {
  if(!get(record, 'isDirty')) return;
  record.send('willCommit');
  var adapter = store.adapterForType(record.constructor);
  commitDetails.get(adapter)[get(record, 'dirtyType')].add(record);
});

Let's walk through it.

forEach(records, function(record) {
  if(!get(record, 'isDirty')) return;

The above says, for each record in the transaction, if it's not dirty, ignore it.

record.send('willCommit');

Otherwise, update its state to inFlight.

var adapter = store.adapterForType(record.constructor);

Get the record's adapter.

commitDetails.get(adapter)

Look up the adapter's created/updated/deleted trio object, which was instantiated at the top of this method here. It's simply an object with the 3 properties created, updated, and deleted, whose values are empty OrderedSets.

[get(record, 'dirtyType')]

Get the appropriate OrderedSet from the object we just obtained. For example, if the record we're on has been updated, get(record, 'dirtyType') will return the string updated. The brackets are just standard JavaScript property lookup, and so it grabs the updated OrderedSet from our trio object in the previous step.

.add(record);

Finally, add the record to the OrderedSet. On subsequent iterations of the loop, we'll add other records of the same type, so all created records get added to one set, all updated records get added to another set, and all deleted records get added to the third set.

What we end up with at the end of the entire method and return from the property is a Map whose keys are adapters, and whose values are these objects with the 3 properties created, updated, and deleted. Each of those, in turn, are OrderedSets of all the records in the transaction that have been created for that adapter, updated for that adapter, and deleted for that adapter, respectively.

Notice that this computed property is marked volatile, so it will get recomputed each time that someone gets the commitDetails property.



来源:https://stackoverflow.com/questions/17499549/where-are-ember-datas-commitdetails-created-updated-deleted-orderedsets-set

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