Skip to content

Instantly share code, notes, and snippets.

@nickschot
Last active July 2, 2018 18:56
Show Gist options
  • Save nickschot/c59e7224470597d5df7189c666762a9f to your computer and use it in GitHub Desktop.
Save nickschot/c59e7224470597d5df7189c666762a9f to your computer and use it in GitHub Desktop.
Setting relationships
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
author1: null,
author2: null,
init(){
this._super(...arguments);
this.set('author1', this.store.createRecord('author', {
name: 'First Author'
}));
this.set('author2', this.store.createRecord('author', {
name: 'Second Author'
}));
},
actions: {
setAuthor1(){
this.model.set('author', this.author1);
},
setAuthor2(){
this.model.set('author', this.author2);
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
comment: belongsTo(),
name: 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: belongsTo(),
text: attr('string')
});
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return this.store.createRecord('comment', {
text: 'This is my comment'
});
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<button {{action 'setAuthor1'}}>Set first author</button>
<button {{action 'setAuthor2'}}>Set second author</button>
<h3>Comment</h3>
<small><b>Author:</b> {{model.author.name}}</small>
<p>{{model.text}}</p>
{
"version": "0.15.0",
"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.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment