Skip to content

Instantly share code, notes, and snippets.

@GCheung55
Created January 10, 2020 20:25
Show Gist options
  • Save GCheung55/7cb7364575c19b582701f96d3e23add6 to your computer and use it in GitHub Desktop.
Save GCheung55/7cb7364575c19b582701f96d3e23add6 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import { inject as service } from '@ember/service';
import { filterBy } from '@ember/object/computed';
export default Ember.Controller.extend({
store: service(),
appName: 'Ember Twiddle',
programTags: filterBy('model.program.tags', 'id', true)
actions: {
deleteTag(tag) {
tag.unloadRecord();
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
title: attr('string'),
tags: hasMany('tag')
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr('string'),
programs: hasMany('program')
});
import Ember from 'ember';
import { inject as service } from '@ember/service';
export default Ember.Route.extend({
store: service(),
beforeModel() {
this.store.pushPayload({
data: [
{
type: 'program',
id: 1,
attributes: {
title: 'Program 1!'
},
relationships: {
tags: {
data: [{type: 'tag', id: 1}, {type: 'tag', id: 3}]
}
}
},
{
type: 'tag',
id: 1,
attributes: { name: 'foo' }
},
{
type: 'tag',
id: 2,
attributes: { name: 'bar' }
},
{
type: 'tag',
id: 3,
attributes: { name: 'baz' }
}
]
})
},
model() {
return {
program: this.store.peekRecord('program', 1),
allTags: this.store.peekAll('tag')
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
Program Title: {{model.program.title}}<br>
Program Tags:<br>
<ul>
{{#each model.program.tags key="id" as | tag |}}
{{#if tag}}
<li>{{tag.name}}</li>
{{/if}}
{{/each}}
</ul>
All Tags:
<ul>
{{#each model.allTags key="id" as | tag |}}
<li>
{{input value=tag.name}}<br>
Programs: {{tag.programs.length}}<br>
<button type="button" {{action "deleteTag" tag}}>Delete</button>
</li>
{{/each}}
</ul>
{
"version": "0.15.1",
"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.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment