Skip to content

Instantly share code, notes, and snippets.

@snewcomer
Last active September 24, 2016 14:23
Show Gist options
  • Save snewcomer/3472d91d9baafed53d13a97d4efc2343 to your computer and use it in GitHub Desktop.
Save snewcomer/3472d91d9baafed53d13a97d4efc2343 to your computer and use it in GitHub Desktop.
/* app/mixins/dirty-check.js */
import Ember from ‘ember’;
var RelationshipDirtyCheck = Ember.Mixin.create({
relationshipDirtyCheck: Ember.computed(‘location’, ‘locationId’, ‘interests.[]’, ‘interestsIds’, function() {
const model = this;
let _dirty = false;
this.eachRelationship((name, meta) => {
if (meta.kind === ‘belongsTo’){
if (model.get(name) && model.get(name).get(‘id’) !== this.get(`${name}Id`)) {
_dirty = true;
}
}
else if (meta.kind === ‘hasMany’) {
const m2m_ids = this.get(`${name}Ids`) || [];
const current_m2m_ids = this.get(name).mapBy(‘id’);
if(m2m_ids.length !== current_m2m_ids.length) {
_dirty = true;
}
m2m_ids.forEach((id) => {
if(!current_m2m_ids.includes(id)) {
_dirty = true;
}
});
}
});
return _dirty;
}),
relationshipsIsDirty: Ember.computed(‘relationshipDirtyCheck’, function(){
return this.get(‘relationshipDirtyCheck’);
}),
relationshipsIsNotDirty: Ember.computed.not(‘relationshipsIsDirty’),
});
export default RelationshipDirtyCheck;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment