Skip to content

Commit

Permalink
Merge normalizeRelationships and setupRelationships methods in store
Browse files Browse the repository at this point in the history
  • Loading branch information
Trent Willis committed Oct 23, 2015
1 parent 4e83729 commit aaa4f04
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ Store = Service.extend({
}
if (data) {
// normalize relationship IDs into records
this._backburner.schedule('normalizeRelationships', this, '_setupRelationships', internalModel, internalModel.type, data);
this._backburner.schedule('normalizeRelationships', this, '_setupRelationships', internalModel, data);
this.updateId(internalModel, data);
}

Expand Down Expand Up @@ -1717,23 +1717,15 @@ Store = Service.extend({
var internalModel = this._load(data);

this._backburner.join(() => {
this._backburner.schedule('normalizeRelationships', this, '_setupRelationships', internalModel, type, data);
this._backburner.schedule('normalizeRelationships', this, '_setupRelationships', internalModel, data);
});

return internalModel;
},

_setupRelationships: function(record, type, data) {
// If the payload contains relationships that are specified as
// IDs, normalizeRelationships will convert them into DS.Model instances
// (possibly unloaded) before we push the payload into the
// store.

data = normalizeRelationships(this, type, data);


// Now that the pushed record as well as any related records
// are in the store, create the data structures used to track
_setupRelationships: function(record, data) {
// This will convert relationships specified as IDs into DS.Model instances
// (possibly unloaded) and also create the data structures used to track
// relationships.
setupRelationships(this, record, data);
},
Expand Down Expand Up @@ -2022,25 +2014,6 @@ Store = Service.extend({

});


function normalizeRelationships(store, type, data, record) {
data.relationships = data.relationships || {};
type.eachRelationship(function(key, relationship) {
var kind = relationship.kind;
var value;
if (data.relationships[key] && data.relationships[key].data) {
value = data.relationships[key].data;
if (kind === 'belongsTo') {
data.relationships[key].data = deserializeRecordId(store, key, relationship, value);
} else if (kind === 'hasMany') {
data.relationships[key].data = deserializeRecordIds(store, key, relationship, value);
}
}
});

return data;
}

function deserializeRecordId(store, key, relationship, id) {
if (isNone(id)) {
return;
Expand Down Expand Up @@ -2110,13 +2083,13 @@ function _commit(adapter, store, operation, snapshot) {
}

function setupRelationships(store, record, data) {
var typeClass = record.type;
if (!data.relationships) {
return;
}

typeClass.eachRelationship((key, descriptor) => {
record.type.eachRelationship((key, descriptor) => {
var kind = descriptor.kind;

if (!data.relationships[key]) {
return;
}
Expand All @@ -2135,7 +2108,11 @@ function setupRelationships(store, record, data) {
relationship = record._relationships.get(key);
relationship.updateMeta(data.relationships[key].meta);
}
var value = data.relationships[key].data;

// If the data contains a relationship that is specified as an ID (or IDs),
// normalizeRelationship will convert them into DS.Model instances
// (possibly unloaded) before we push the payload into the store.
var value = normalizeRelationship(store, key, descriptor, data.relationships[key]);

if (value !== undefined) {
if (kind === 'belongsTo') {
Expand All @@ -2149,5 +2126,18 @@ function setupRelationships(store, record, data) {
});
}

function normalizeRelationship(store, key, relationship, value) {
var data = value.data;
if (data) {
var kind = relationship.kind;
if (kind === 'belongsTo') {
return value.data = deserializeRecordId(store, key, relationship, data);
} else if (kind === 'hasMany') {
return value.data = deserializeRecordIds(store, key, relationship, data);
}
}
return data;
}

export { Store };
export default Store;

0 comments on commit aaa4f04

Please sign in to comment.