Last active
October 13, 2018 04:50
-
-
Save leondmello/a5da5d0e3dbfd8e40ef7479f5818ddb4 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
import DS from 'ember-data'; | |
import ModelMixin from 'ember-data-extensions/mixins/model'; | |
import { memoize, startCase } from 'lodash-es'; | |
import { get, set } from '@ember/object'; | |
export default class ApplicationModel extends DS.Model.extend(ModelMixin).reopenClass({ | |
attributeLabels: memoize( | |
function (this: ApplicationModel, store: any): object { | |
const labelMap = { | |
self: this.modelViewName() | |
}; | |
this.eachRelationship((name: string, rel: any) => labelMap[name] = store.modelFor(rel.type).modelViewName()); | |
this.eachAttribute((name: string) => labelMap[name] = startCase(name)); | |
return Object.assign(labelMap, this.overriddenAttributeLabels(store)); | |
}, function (this: ApplicationModel): string { | |
return this.modelName; | |
} | |
), | |
modelViewName: memoize( | |
function (this: ApplicationModel): string { | |
return this.overriddenModelViewName() || startCase(this.modelName); | |
}, function (this: ApplicationModel): string { | |
return this.modelName; | |
} | |
), | |
overriddenModelViewName: (): string => { return ''; }, | |
overriddenAttributeLabels: (_store: any): object => { return {}; } | |
}) { | |
save(this: ApplicationModel, options: object) { | |
const saveOpts = options ? options : { resetRelations: false }; | |
return this._super(saveOpts); | |
}; | |
setDefaults(this: ApplicationModel, attrs: string[]) { | |
const defaultRecord = get(this, 'store').createRecord(this.constructor.modelName); | |
if (attrs) { | |
attrs.forEach((attr) => set(this, attr, defaultRecord.get(attr))); | |
} else { | |
this.eachAttribute((attr: string) => set(this, attr, defaultRecord.get(attr))); | |
} | |
defaultRecord.deleteRecord(); | |
return this; | |
}; | |
serializeWithRelationships(this: ApplicationModel, relationships: object) { | |
return this._internalModel.createSnapshot({ adapterOptions: { relationships } }).serialize(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment