Skip to content

Instantly share code, notes, and snippets.

@jelhan
Created September 7, 2020 09:39
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 jelhan/67b8c1dd349a0c02c9ff9d23341dacae to your computer and use it in GitHub Desktop.
Save jelhan/67b8c1dd349a0c02c9ff9d23341dacae to your computer and use it in GitHub Desktop.
Executing methods on Ember Data modls in template
import Model, { attr } from '@ember-data/model';
export default class extends Model {
@attr('string')
title;
@attr('string')
body;
}
import Route from '@ember/routing/route';
export default Route.extend({
async model() {
this.store.pushPayload({
data: {
id: '1',
type: 'articles',
attributes: {
title: 'Example',
body: 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo',
},
},
});
return this.store.peekRecord('article', '1');
}
});
{{#let @model as |article|}}
<h1>Edit article {{article.id}}</h1>
<form>
<div>
<label for="title">Title:</label>
<input
id="title"
value={{article.title}}
{{on "input" (action (mut article.title) value="target.value")}}
>
</div>
<div>
<label for="body">Body:</label>
<textarea
id="body"
{{on "input" (action (mut article.body) value="target.value")}}
>{{article.body}}</textarea>
</div>
<button
type="button"
{{on "click" (bind article.rollbackAttributes target=article)}}
>
reset changes
</button>
</form>
{{/let}}
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-bind-helper": "1.0.0",
"ember-data": "3.18.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment