-
-
Save alexdiliberto/75d037451c5f354a471d17fd3f2c7ae2 to your computer and use it in GitHub Desktop.
To use, say you have a `lesson` from a route's `model` hook. Then you could pass `lesson` into a `{{lesson-form}}`. The form could then `lesson.createDraft()` and pass that around to all the inputs. When the form calls `draft.save()`, upon completion the adapter layer rolls the successfully-persisted changes back into the original `lesson` model.
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
// models/draft/lesson.js | |
export { default } from '../lesson'; |
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/adapters/drafts/base.js | |
import ApplicationAdapter from '../application'; | |
export default ApplicationAdapter.extend({ | |
pathForType(...args) { | |
let path = this._super(...args); | |
return path.replace('drafts/', ''); | |
}, | |
createRecord: function(store, type, snapshot) { | |
return this._super(store, type, snapshot) | |
.then((payload) => { | |
let modelName = type.modelName.replace('drafts/', ''); | |
store.pushPayload(modelName, payload); | |
}); | |
}, | |
updateRecord: function(store, type, snapshot) { | |
return this._super(store, type, snapshot) | |
.then(payload => { | |
let modelName = type.modelName.replace('drafts/', ''); | |
store.pushPayload(modelName, payload); | |
}); | |
}, | |
deleteRecord(store, type, snapshot) { | |
return this._super(store, type, snapshot) | |
.then(() => { | |
let modelName = type.modelName.replace('drafts/', ''); | |
let record = store.peekRecord(modelName, snapshot.id); | |
store.unloadRecord(record); | |
}); | |
} | |
}); |
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/adapters/drafts/lesson.js | |
import DraftBase from './base'; | |
export default DraftBase; |
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
// models/lesson.js | |
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
createDraft() { | |
const id = this.get('id'); | |
const attrs = Ember.merge(this.serialize(), {id}); | |
this.get('store').pushPayload('drafts/lesson', {'drafts/lesson': attrs}); | |
return this.get('store').peekRecord('drafts/lesson', id); | |
}, | |
discardDraft(draft) { | |
this.get('store').unloadRecord(draft); | |
}, | |
discardDrafts() { | |
let lessonDraft = this.get('store').peekRecord('drafts/lesson', this.get('id')); | |
if (lessonDraft) { | |
this.get('store').unloadRecord(lessonDraft); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment