Skip to content

Instantly share code, notes, and snippets.

@yohanmishkin
Last active May 23, 2018 00:59
Show Gist options
  • Save yohanmishkin/3dab3c1f172ffbccd4d4e31200ad2250 to your computer and use it in GitHub Desktop.
Save yohanmishkin/3dab3c1f172ffbccd4d4e31200ad2250 to your computer and use it in GitHub Desktop.
rich vs. anemic domain modeling
import Ember from 'ember';
export default Ember.Controller.extend({
store: Ember.inject.service(),
post: null,
init() {
let post = this.get('store').createRecord('post');
this.set('post', post);
},
actions: {
addComment() {
let post = this.get('post');
let comment = this.get('store').createRecord('comment', { content: 'hiya!' });
post.add(comment);
},
addCommentAnemically() {
let post = this.get('post');
let comment = this.get('store').createRecord('comment', { content: 'hello!' });
post.get('comments').pushObject(comment);
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
content: attr('string')
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
author: attr('string'),
description: attr('string'),
comments: hasMany(),
add(comment) {
this.get('comments').pushObject(comment);
}
});
<button {{action 'addComment'}}>Add comment</button>
<button {{action 'addCommentAnemically'}}>Add comment anemically</button>
<ul>
{{#each post.comments as |comment|}}
<li>{{comment.content}}</li>
{{/each}}
</ul>
{
"version": "0.13.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment