Skip to content

Instantly share code, notes, and snippets.

@JonForest
Last active September 16, 2016 04:41
Show Gist options
  • Select an option

  • Save JonForest/d681b08440d24cf13ffb93e29858349e to your computer and use it in GitHub Desktop.

Select an option

Save JonForest/d681b08440d24cf13ffb93e29858349e to your computer and use it in GitHub Desktop.
const FormObject = Ember.Object.extend({
toJSON() {
// get a pure POJO
const tempObj = JSON.parse(JSON.stringify(this))
tempObj.isDirty.delete
return tempObj
},
isDirty: false,
set(key, value) {
this._super(...arguments)
// any set, even with the same value, will fire the isDirty flag
set(this, 'isDirty', true)
}
})
// --- route.js ---
// Note: much of this should be pulled into a mixin for re-use across all edit routes
afterModel(model) {
let formObject
const id = get(model, 'id')
const modelName = get(model, 'constructor.modelName')
model = {model: model}
return RSVP.promise(get(this, 'store').query('draft', {filter: {where: {modelId: id, modelType: modelName}}))
.then(draft => {
set(model, 'draft', draft)
if (draft) {
set(model, 'canDiscard', true)
modelJson = get(draft, 'modelJson')
} else {
set(model, 'canDiscard', true)
modelJson = get(model, 'model').toJSON()
}
return FormObject.create(modelJson)
})
.error(() => {
// Error while fetching a draft, abandon it. If we logged JS errors, we should do it here
return get(model, 'model').toJSON()
})
.finally(formObject => {
set(model, 'formObject', formOject)
this.automaticSaveTimer()
})
},
automaticSaveTimer() {
const interval = 2 * 60 * 1000
Ember.run.later(this, function () {
get(this, 'automaticSaveTask').peform(model)
this.automaticSaveTimer()
}, interval)
},
manualSaveTask: task(function * (model) {
return model.save()
.then(() => {
set(this, 'controller.model.canDiscard', false)
//etc.
})
}).drop(), // see http://ember-concurrency.com/#/docs/task-concurrency
automaticSaveTask: task(function * (formObject) {
let draftModel = get(this, 'controller.model.draft')
if (draftModel) set(draftModel, 'modelJson', formObject.toJSON()) // or JSON.stringify(formObject) maybe
else {
draftModel = get(this, 'store').createRecord('draft', {
modelId: get(this, 'controller.model.model.id'),
modelType: get(this, 'controller.model.model.constructor.modelName'),
modelJson: formObject.toJSON(),
user: get(this, 'session.currentUser')
})
}
return draftModel.save()
.then() // if you want to
}).drop(),
actions: {
manualSave (model} {
// todo: this needs checking, because we need to ensure that the local store for the proper model is appropriately updated
get(this, 'manualSaveTask').perform(model)
},
discard () {
const model = get(this, 'model.model')
set(this, 'controller.model.formObject', model.toJSON())
}
}
// --- template.hbs ---
// Will want some logic around whether to display the 'keep editing' 'another user editing' etc logic here
{{your-component
item=model.formObject
save=(route-action 'manualSave')
discard=(route-action 'discart')
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment