Hey all!
I have a model named Module
. It looks like this:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
modules: DS.hasMany('modules', { inverse: null}),
});
Modules form a tree: there’s a “crate” model at the root, and it can contain zero or more modules, and then each module can contain zero or more child modules. It’s always a tree, not a general graph.
This is working great. My modules can see their children, everything works.
However, I want a link to the parent module from the child.
I have JSON that looks like this. As you can see, there’s also a “crate” module that has modules, this is the root of the tree.
{
"data": {
"type": "crate",
"id": "example",
"relationships": {
"modules": {
"data": [
{
"type": "module",
"id": "example::nested1"
}
]
}
}
},
"included": [
{
"type": "module",
"id": "example::nested1",
"attributes": {
"name": "nested1",
"docs": " nested 1\n"
},
"relationships": {
"modules": {
"data": [
{
"type": "module",
"id": "example::nested1::nested2"
}
]
}
}
},
{
"type": "module",
"id": "example::nested1::nested2",
"attributes": {
"name": "nested2",
"docs": " nested 2\n"
},
"relationships": {
"modules": {
"data": [
{
"type": "module",
"id": "example::nested1::nested2::nested3"
}
]
}
}
},
{
"type": "module",
"id": "example::nested1::nested2::nested3",
"attributes": {
"name": "nested3",
"docs": " nested 3\n"
}
}
]
}
I cut out relevant context from the actual json, so hopefully I didn’t screw it up. As I said, this should be working for traversing down the tree.
and I tried adding something like this to my model, but it doesn’t work. No surprise as I’m pretty much just flailing here:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
modules: DS.hasMany('modules', { inverse: null}),
parent_module: DS.belongsTo('module', { inverse: 'modules'}),
});
Any way I can do this? I’ve also though, if ember-data can’t handle it, my route could try to look stuff up, since any module belongs to one parent?
Thanks!