Ember Data internals - prematerialized data



I’m rewriting an app to the newest version of Ember Data and I’ve noticed that Store.load() method changed signature and it looks as follows: load: function(type, data, prematerialized).

What is the difference between data and prematerialized? From a quick look over the code and a few experiments, it seems that this method will still work as previously when you pass an id:

store.load(App.Post, 1, { id: 1, title: 'A post' });

After some more investigation I noticed that the RESTAdapter inserts the JSON data as “data”, so it will essentially call something like:

store.load(App.Post, { id: 1, title: 'A post' }, { id: 1 });
// the second argument is always an object with just an id in the RESTAdapter

After loading the record in such way, the prematerialized data can be further extended with an embedded relationships and finaly it’s saved to the clientIdToPrematerializedData array.

Now, when a record is materialized the algorithm is as follows:

  1. materialize attributes, for each attribute do:
  • check if the prematerialized object has the given attribute
  • if yes, load the attribute directly into the attributes hash (ie. no transform like first_name -> firstName)
  • if no, extract the attribute and deserialize it, only then load the attribute into the attributes hash
  1. materialize relationships similarly (ie. prioritize prematerialized attributes)

After such investigation it seems to me that when loading data into the store from external sources, one should generally use the second argument (ie. data) ofloadmethod.prematerialized` data should be used if we have newer data already available in the app, already in deserialized form and with naming used already in the model.

It also looks like this was changed to make it easier to transform and deserialize data on materialization, not before loading it into the store, am I right?

Could anyone more familiar with DS.Store confirm/deny those findings, add anything I’ve missed and fix any misunderstandings?


Exactly. If you load in raw data, Ember Data will wait until you actually try to materialize the record before processing it. In some cases (for example with embedded records), it’s actually critical to pre-process this data; otherwise, the app would have no way to know that the embedded record was already available (and may try to load it from the server).