Skip to content

Instantly share code, notes, and snippets.

@jenweber
Last active February 2, 2019 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenweber/03c1c3ecf3bd50e4751269ec9bfd900c to your computer and use it in GitHub Desktop.
Save jenweber/03c1c3ecf3bd50e4751269ec9bfd900c to your computer and use it in GitHub Desktop.
The simplest CRUD possible with Ember Data
import Controller from '@ember/controller';
// a controller is a place to define 'actions', such as the things that should happen
// when a user clicks a button to create/read/edit/delete a record
export default Controller.extend({
actions: {
createBoardGame() {
// get the input value from the .hbs template
let newBoardGame = this.get('newBoardGame')
// create a record in Ember Data (locally, would not survive page refresh)
let newRecord = this.store.createRecord('boardgame', {
title: newBoardGame
})
// Save the record to the API endpoint specified in adapters/application.js
newRecord.save()
},
readBoardGame() {
// get the input value from the .hbs template
let id = this.get('boardGameId')
// find the record (cheating and using id 1 from my mocked server)
this.store.findRecord('boardgame', 1).then((game) => {
alert(game.get('title') + ' ' + game.get('id'))
})
},
updateBoardGame() {
let updatedTitle = this.get('updatedTitle')
let game = this.get('model').findBy('id', '1')
game.set('title', updatedTitle) // locally update the title
game.save(); // save the title to API via PATCH
},
destroyBoardGame() {
let destroyId = this.get('destroyId')
let game = this.get('model').findBy('id', destroyId)
game.destroyRecord() // destroy deletes & saves in one step
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment