Skip to content

Instantly share code, notes, and snippets.

@luxzeitlos
Last active January 26, 2016 15:26
Show Gist options
  • Save luxzeitlos/a458cf9fcec51786e6e8 to your computer and use it in GitHub Desktop.
Save luxzeitlos/a458cf9fcec51786e6e8 to your computer and use it in GitHub Desktop.
SO hasMany update demo
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
actions: {
addChild() {
this.store.createRecord('child', {
parent: this.get('model'),
name: this.get('newChildName')
});
this.set('newChildName', '');
}
}
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
let parent = this.store.createRecord('parent', {
id: '1'
});
this.store.createRecord('child', {
name: 'first child',
parent: parent
});
return this.store.find('parent', '1');
}
});
<h1>Welcome to {{appName}}</h1>
<br>
new Child: {{input value=newChildName}}
<button {{action "addChild"}}>add Child</button>
<br>
id: {{model.id}}
<br>
<hr />
<br>
{{#each model.childs as |c|}}
Child: {{c.name}}
<br>
{{/each}}
import DS from 'ember-data';
export default DS.Model.extend({
parent: DS.belongsTo('parent'),
name: DS.attr('string')
});
import DS from 'ember-data';
export default DS.Model.extend({
childs: DS.hasMany('child')
});
{
"version": "0.5.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment