Last active
September 24, 2016 14:23
-
-
Save snewcomer/3472d91d9baafed53d13a97d4efc2343 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* 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