Last active
August 27, 2019 11:12
-
-
Save adriancooney/f3d3b01868619e67799ce18433375ac5 to your computer and use it in GitHub Desktop.
Ember save / set
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
import DS from 'ember-data'; | |
export default DS.JSONAPIAdapter.extend({ | |
updateRecord(store, type, { record }) { | |
console.log("Saving draft", record.id); | |
return sleep(1000); | |
}, | |
createRecord(store, type, { record }) { | |
console.log("Creating draft", record.body); | |
return sleep(1000).then(() => ({ | |
data: { | |
type: "draft", | |
id: Math.floor(Math.random()*1000), | |
attributes: { | |
body: record.body, | |
}, | |
}, | |
})); | |
}, | |
findRecord(store, type, id, snapshot) { | |
console.log("Finding draft", id); | |
return sleep(1000).then(() => ({ | |
data: { | |
type: "draft", | |
id: id.toString(), | |
attributes: { | |
body: "", | |
}, | |
}, | |
})); | |
}, | |
}); | |
async function sleep(timeout) { | |
await new Promise(resolve => setTimeout(resolve, timeout)); | |
} |
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
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
async init() { | |
await this.exampleReloadCreate(); | |
}, | |
async exampleFindRecord() { | |
let draft = this.store | |
}, | |
async exampleReloadCreate() { | |
let draft = this.store.createRecord('draft', { | |
body: "", | |
}); | |
printModelState(draft); | |
let draftSave = draft.save(); | |
printModelState(draft); | |
await draftSave; | |
printModelState(draft); | |
}, | |
async exampleReload() { | |
let draft = this.pushDraft(); | |
let draftReload = draft.reload(); | |
await sleep(100); | |
draft.set('body', 'foo'); | |
await draftReload; | |
}, | |
async exampleSave() { | |
let draft = this.pushDraft(); | |
let draftSave = draft.save(); | |
await sleep(100); | |
draft.set('body', 'foo'); | |
await draftSave; | |
console.log('draft body post-save', draft.body); | |
}, | |
pushDraft() { | |
this.store.push({ | |
data: [{ | |
type: 'draft', | |
id: 1, | |
attributes: { | |
body: "", | |
}, | |
}] | |
}); | |
return this.store.peekRecord('draft', 1); | |
}, | |
}); | |
function printModelState(model) { | |
console.log('Model state:', model._internalModel.currentState.stateName); | |
} | |
async function sleep(timeout) { | |
await new Promise(resolve => setTimeout(resolve, timeout)); | |
} |
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
import Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import { belongsTo, hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
body: attr(), | |
}); |
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
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment