Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflexive relationship not working #5159

Closed
camarrone opened this issue Aug 30, 2017 · 1 comment
Closed

Reflexive relationship not working #5159

camarrone opened this issue Aug 30, 2017 · 1 comment

Comments

@camarrone
Copy link

camarrone commented Aug 30, 2017

Hello,

I'm trying to set a reflexive relationship that will be used for a d3js graph (tree view) but I keep getting the following error in my component:

Assertion Failed: You can no longer pass a modelClass as the first argument to store._buildInternalModel. Pass modelName instead.

The error is raised every time I try to access the "children" in the model. My backend is returning the following payload:

[
{  
   "id": "1",
   "name":"D3",
   "children":[  
      {  
         "id": "c1",
         "name":"Drivers",
         "children":[  
            {  
               "id": "c1-1",
               "name":"JavaScript",
               "size":17010
            },
            {  
               "id": "c1-2",
               "name":"SVG",
               "size":5842
            },
            {  
               "id": "c1-3",
               "name":"CSS",
               "size":5842
            },
            {  
               "id": "c1-4",
               "name":"Also HTML",
               "size":5842
            }
         ]
      },
      {  
          "id": "c2",
          "name":"Animation",
          "children":[  
             {  
                "id": "c2-1",
                "name":"CSS3 Transitions",
                "children":[  
                   {  
                      "id": "c2-1-1",
                      "name":"Chaining",
                      "size":3938
                   },
                   {  
                      "id": "c2-1-2",
                      "name":"Tweening",
                      "size":3938
                   }
                ]
            }
         ]
       },
   ]
}
]

this my "node" model:

export default DS.Model.extend({
	name: DS.attr('string'),
	parent: DS.belongsTo('node', {inverse: 'children'}),
	children: DS.hasMany('node', {inverse: 'parent'}),
});

this is my route:

export default Ember.Route.extend({
	model() {
		//return this.store.findAll('node');
	    return RSVP.hash({
	      node: this.store.findAll('node'),
	    });
	},
});

and this is the component where I'm using this model:

export default Ember.Component.extend({
	store: Ember.inject.service(),

	didRender(){
		let rootNode = this.get('store').peekRecord('node',1); 

		let rootChildren = rootNode.get('children');
		console.log(rootChildren);
	},
});

Every time I try that rootNode.get('children') , I get the error mentioned above.

If I go to the Ember Inspector and go to "data", select model "node" and then click on the computed property "children" I get the following erro in the console:

Object Inspector error for children Error
    at Object.assert (http://localhost:4200/assets/vendor.js:24291:15)
    at Object.assert (http://localhost:4200/assets/vendor.js:82122:34)
    at Class._buildInternalModel (http://localhost:4200/assets/vendor.js:94483:21)
    at Class._internalModelForId (http://localhost:4200/assets/vendor.js:93196:19)
    at Class._pushResourceIdentifier (http://localhost:4200/assets/vendor.js:94657:17)
    at Class._pushResourceIdentifiers (http://localhost:4200/assets/vendor.js:94668:33)
    at ManyRelationship.updateData (http://localhost:4200/assets/vendor.js:86875:37)
    at ManyRelationship.push (http://localhost:4200/assets/vendor.js:86185:12)
    at Relationships.get (http://localhost:4200/assets/vendor.js:87230:22)
    at Class.get (http://localhost:4200/assets/vendor.js:95149:49)

I went through a lot of docs and links but I can't get this working.

Is this a bug or am I doing anything wrong here?

Thanks very much.

@camarrone camarrone changed the title Reflexive relationship not workin Reflexive relationship not working Aug 30, 2017
@pangratz
Copy link
Member

pangratz commented Oct 7, 2017

It's not obvious from your description, but with a payload like that, you need to use the EmbeddedRecordsMixin. See this ember-twiddle for a working example with the payload you posted.

Since this is working as expected, I am closing. Please open up a new issue if you are still running into a bug, preferable with a failing test case / ember twiddle, demonstrating the issue you're having. If you have further questions, feel free to chime in at the -ember-data channel in the Ember Community Slack.

@pangratz pangratz closed this as completed Oct 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants