Skip to content

Instantly share code, notes, and snippets.

@Sinled
Last active June 20, 2016 08:38
Show Gist options
  • Save Sinled/ab180cffeddeda454721f8a7d489ed96 to your computer and use it in GitHub Desktop.
Save Sinled/ab180cffeddeda454721f8a7d489ed96 to your computer and use it in GitHub Desktop.
test-delete-poly-record-with-relations
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
deleteRecord(item) {
item.deleteRecord();
alert('delete');
},
unloadRecord(item) {
item.unloadRecord();
alert('unload');
},
}
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import MyModel from './my-model';
export default MyModel.extend({
somePolyField: attr('string', { defaultValue: 'hello from poly' }),
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
someField: attr('number', { defaultValue: 1 }),
parent: belongsTo('parent-model'),
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
someNormalField: attr('string', { defaultValue: 'hello from normal' }),
parent: belongsTo('parent-model'),
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import Ember from 'ember';
export default Model.extend({
myPolymorphicModels: hasMany('my-model', { async: false }),
myNormalModels: hasMany('normal-model', { async: false }),
myPolymotphicRecord: Ember.computed('myPolymorphicModels.[]', function() {
return this.get('myPolymorphicModels.firstObject');
}),
myNormalRecord: Ember.computed('myNormalModels.[]', function() {
return this.get('myNormalModels.firstObject');
}),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
const parent = this.store.createRecord('parent-model');
const polymorhicRecord = this.store.createRecord('my-model-poly', { parent });
const normalRecord = this.store.createRecord('normal-model', { parent });
parent.get('myNormalModels').addObject(normalRecord);
parent.get('myPolymorphicModels').addObject(polymorhicRecord);
return {
parent,
};
},
});
<h3>Poymorhic record</h3>
via parent: {{model.parent.myPolymotphicRecord.somePolyField}}, <br>
new: {{model.parent.myPolymotphicRecord.isNew}}, <br>
deleted: {{model.parent.myPolymotphicRecord.isDeleted}}
<hr>
<h3>Normal record</h3>
via parent: {{model.parent.myNormalRecord.someNormalField}}, <br>
new: {{model.parent.myNormalRecord.isNew}}, <br>
deleted: {{model.parent.myNormalRecord.isDeleted}}
<hr>
<button {{action 'deleteRecord' model.parent.myNormalRecord}}>deleteNormalRelation</button>
<button {{action 'deleteRecord' model.parent.myPolymotphicRecord}}>deletePolymorphicRelation</button>
<button {{action 'unloadRecord' model.parent.myPolymotphicRecord}}>unload polymorphic relation</button>
<br>
<br>
{
"version": "0.9.2",
"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.6.0",
"ember-data": "2.6.0",
"ember-template-compiler": "2.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment