Skip to content

Instantly share code, notes, and snippets.

@danielspaniel
Last active March 18, 2017 02:45
Show Gist options
  • Save danielspaniel/5fa2844404c7f9bc3ecba0e6ed79c605 to your computer and use it in GitHub Desktop.
Save danielspaniel/5fa2844404c7f9bc3ecba0e6ed79c605 to your computer and use it in GitHub Desktop.
polymorphic belongTo
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddles',
thingIds: Ember.computed.mapBy('model.things', 'id'),
thingState: Ember.computed.mapBy('model.things', 'isDeleted')
});
<script src="https://cdn.jsdelivr.net/jquery.mockjax/2.2.1/jquery.mockjax.min.js" type="text/javascript"></script>
import Person from "./person";
import attr from "ember-data/attr";
export default Person.extend({
type: attr('string', { defaultValue: 'BigPerson' })
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
type: attr('string'),
things: hasMany('thing')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
person: belongsTo('person', {polymorphic: true})
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
let data = {
data: {
id: 1,
type: 'big-person',
relationships: {
things: {
data: [
]
}
}
},
included: [
//{id: 1, type: "thing"},
//{id: 2, type: "thing"}
]
};
this.store.pushPayload(data);
},
model() {
let person = this.store.peekRecord('person', 1);
$.mockjax({
url: '/things',
type: 'POST',
responseText: {thing: {id: 1, person: {type: 'big-person', id: 1}}}
});
let thing = this.store.createRecord('thing', {person: person});
thing.save()
return thing;
}
});
<html>
<head>
</head>
<body>
<h3>Welcome to Bug where pushPaylod does not clear hasMany relationship</h3>
<br>
My thing:{{model}}
<br>
I should have 0 things but I have:{{model.things.length}}
{{outlet}}
<br>
<br>
</body>
</html>
{
"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",
"mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js",
"ember": "release",
"ember-data": "release",
"ember-template-compiler": "release",
"ember-testing": "release"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment