Created
August 12, 2016 17:31
-
-
Save robbiespeed/af845598a37f3ba7d84e3ac17f8b792d to your computer and use it in GitHub Desktop.
Ember-Data dirtyRelationships
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import Model from 'ember-data/model'; | |
const { computed } = Ember; | |
function checkDirtyRelationships () { | |
const dirtyRelationships = this.get('_dirtyRelationships'); | |
let anyDirty = false; | |
this.eachRelationship((key, descriptor) => { | |
// only care about hasMany and belongsTo | |
if (descriptor.kind === 'hasMany' || descriptor.kind === 'belongsTo') { | |
// use kind to either grab from hasMany or belongsTo | |
const { canonicalMembers, members } = | |
this[descriptor.kind](key)[descriptor.kind + 'Relationship']; | |
// if it's dirty store the relationship diff in changedRelationships | |
if ( | |
// (either canonicalMembers or members do exist) AND | |
(canonicalMembers || members) && | |
// (canonicalMembers does not equal members) AND | |
(canonicalMembers !== members) && | |
// (size isn't equal) OR | |
(canonicalMembers.size !== members.size) || | |
// any members missing from members | |
canonicalMembers.toArray().any((member) => !members.has(member)) | |
) { | |
anyDirty = true; | |
dirtyRelationships.set(key, descriptor); | |
} | |
// else if not dirty delete the relationship diff in changedRelationships | |
else if (dirtyRelationships.has(key)) { | |
dirtyRelationships.delete(key); | |
} | |
} | |
}); | |
return anyDirty; | |
} | |
export function initialize(/* application */) { | |
Model.reopen({ | |
init () { | |
this._super(...arguments); | |
const dependencies = []; | |
this.eachRelationship((key, descriptor) => { | |
if (descriptor.kind === 'hasMany') { | |
dependencies.push(key + '.[]'); | |
} | |
else if (descriptor.kind === 'belongsTo') { | |
dependencies.push(key); | |
} | |
}); | |
this.set('_dirtyRelationships', new Map()); | |
this.set('hasDirtyRelationships', | |
computed(...dependencies, checkDirtyRelationships) | |
); | |
}, | |
changedRelationships () { | |
let changedRelationships = {}; | |
if (this.get('hasDirtyRelationships')) { | |
for (const [key, descriptor] of this.get('_dirtyRelationships')) { | |
const { canonicalMembers, members } = | |
this[descriptor.kind](key)[descriptor.kind + 'Relationship']; | |
changedRelationships[key] = [canonicalMembers, members]; | |
} | |
} | |
return changedRelationships; | |
} | |
}); | |
} | |
export default { | |
name: 'model-reopen', | |
initialize | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not so stoked on setting the
hasDirtyRelationships
computed prop in the init, but adding a different computed prop to each Model is not really acceptable (or scalable) IMO.