Skip to content

Instantly share code, notes, and snippets.

@Sinled
Last active August 28, 2018 18:39
Show Gist options
  • Save Sinled/c602826395610b6245cd4e9147380831 to your computer and use it in GitHub Desktop.
Save Sinled/c602826395610b6245cd4e9147380831 to your computer and use it in GitHub Desktop.
deleteRecord-on-polymorhic-models
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
deleteRelation(relation) {
relation.deleteRecord();
}
}
});
import Pet from "./pet";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Pet.extend({
});
import Pet from "./pet";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Pet.extend({
});
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('strign'),
pets: hasMany('pet', { polymorphic: true }),
toys: hasMany('toy'),
});
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'),
person: belongsTo('person'),
});
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'),
person: belongsTo('person'),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
const person = this.store.createRecord('person', {
name: 'Jhon',
});
const cat = this.store.createRecord('cat', { name: 'Catty', person });
const dog = this.store.createRecord('dog', { name: 'Doggy', person });
person.get('pets').addObjects([cat, dog]);
const batman = this.store.createRecord('toy', { name: 'batman', person });
const superman = this.store.createRecord('toy', { name: 'superman', person })
person.get('toys').addObjects([batman, superman]);
return person;
}
});
<h1>Welcome polymorphic models:</h1>
<br>
<p>When you try to delete polymorhic model (pet) it stays in relation and store, after calling on them <code>deleteRecord()</code></p>
<hr>
<h4>Person: {{model.name}}, has {{model.pets.length}} polymorhic pets:</h4>
<ul>
{{#each model.pets as |pet|}}
<li>
<b>{{pet.name}}</b>,
isNew pet: <b>{{pet.isNew}}</b>,
isDeleted pet: <b>{{pet.isDeleted}}</b>,
<button onclick={{action 'deleteRelation' pet}}>delete {{pet.name}}</button>
</li>
{{/each}}
</ul>
<br>
<hr>
<p>On contrary when you delete defaul relation it is deleted from parent record and from store</p>
<h4>Person: {{model.name}}, has {{model.toys.length}} default hasMany toys:</h4>
<ul>
{{#each model.toys as |toy|}}
<li>
<b>{{toy.name}}</b>,
isNew pet: <b>{{toy.isNew}}</b>,
isDeleted pet: <b>{{toy.isDeleted}}</b>,
<button onclick={{action 'deleteRelation' toy}}>delete {{toy.name}}</button>
</li>
{{/each}}
</ul>
<br>
<hr>
{{outlet}}
<br>
<br>
import { run } from '@ember/runloop';
export default function destroyApp(application) {
run(application, 'destroy');
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
import { start } from 'ember-cli-qunit';
setResolver(resolver);
start();
{
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "3.3.2",
"ember-template-compiler": "release",
"ember-testing": "release"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment