Last active
September 23, 2019 19:28
-
-
Save betocantu93/ce1730843fc2d18aca5beab4961a37b7 to your computer and use it in GitHub Desktop.
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
copyToNoneLocal: task(function* (draft, ignoreModel = null, level = 0) { | |
try { | |
let draftType = draft.modelName; | |
let modelType = draftType.replace('-local', ''); | |
let model = this.store.createRecord(modelType); | |
let mixedProperties = []; | |
let relationships = []; | |
let properties = [] | |
//get mixedProperties from non local version (this include belongsTo relationships) | |
mixedProperties = Object.keys(model.toJSON()); | |
//get relationships | |
draft.eachRelationship((rel) => { | |
relationships.push(rel) | |
if (!mixedProperties.includes(rel)) { mixedProperties.push(rel) } | |
}); | |
//get noneRelationships properties | |
properties = mixedProperties.filter((item) => { | |
return !relationships.includes(item); | |
}) | |
//set noneRelationships properties to model | |
model.setProperties(draft.getProperties(properties)); | |
let a = draft.getProperties(relationships); | |
for (let key in a) { | |
if (a[key].content !== null && a[key].content !== undefined) { | |
if (a[key].content._internalModel !== undefined) { | |
// a[key] is the associated model | |
if (a[key].content._internalModel.modelName.includes('local')) { | |
if (a[key].content._internalModel.modelName != ignoreModel) { | |
// window.console.log(`found local relationship on ${a[key].content._internalModel.modelName}, copying`); | |
let record = yield this.copyToNoneLocal.perform(a, draftType, level += 1) | |
model.set(key, record) | |
} | |
} else { | |
model.set(key, a[key]); | |
} | |
} else { | |
//a[key] is the associated collection | |
if (a[key].content.type.modelName.includes('local')) { | |
if (a[key].content.type.modelName != ignoreModel && a[key].get('length') > 0) { | |
// window.console.log(`found local relationship on ${a[key].content.relationship.relationshipMeta.type}, will copy ${a[key].get('length')} objects`); | |
let promises = [] | |
a[key].forEach((a) => { | |
promises.push(this.copyToNoneLocal.perform(a, draftType, level += 1)); | |
}) | |
let records = yield all(promises) | |
model.set(key, records); | |
} | |
} else { | |
model.set(key, a[key]); | |
} | |
} | |
} | |
} | |
return model | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment