Is a self referencing many to many relationship possible? (via fixtures)


#1

For the last 2 hours i tried to define a self referencing relationship in ember data - without any luck.

Idea is simple: Be able to have one item refer to multiple other items of the same class.

I tried every possible combination i could come up with, always just resulting it not helpful error messages like so:

Uncaught #<error> VM21795:956
ProfileManager.ended VM21795:956 
Ember.subscribe.after VM21795:2007
finalizer ember.js:1802
Ember.tryCatchFinally ember.js:1554
Ember.Instrumentation.instrument ember.js:1810
Ember.CoreView.Ember.Object.extend.renderToBuffer ember.js:22472
Ember.merge.renderToBufferIfNeeded ember.js:24960
Ember.View.Ember.CoreView.extend.renderToBufferIfNeeded ember.js:24169
Ember.merge.ensureChildrenAreInDOM ember.js:25622
   Ember.ContainerView.Ember.View.extend._ensureChildrenAreInDOMember.js:25587
DeferredActionQueues.flush ember.js:6127
Backburner.end ember.js:6215
Backburner.run ember.js:6254
executeTimers ember.js:6552
(anonymous function) ember.js:6542

This is my code (at least one version of it):

var Location = DS.Model.extend({
  name: DS.attr(),
  to: DS.hasMany('location', {inverse: 'to'})
});

Location.reopenClass({
  FIXTURES: [
    {
      id: "start",
      name: "Your Room",
      to: ["left"]
    }, {
      id: "left",
      name: "Left",
      to: ["start"]
    }
  ]
});

I have tried with/without the inverse definition. With a different inverse than the main relationship (with and without ids for the back reference). I tried different names for it. And on and on for the past 2 hours…

Should ember-data be able to handle this case at all? And if so how? I’d really like to stick to ember-data as i have used it in the past quite happily.

I’m using: Ember 1.5.1 with Ember Data 1.0.0-beta.8.2a68c63a (App Kit)


#2

I don’t have a solution to do it the way you mention, but my RESTful intuition is that you should have a separate resource: “roomConnection”. Then if two rooms are connected, they belong to the same roomConnection instance. Maybe you thought of this, but just throwing it out there as a solution.


#3

It might end up this way as i need the additional logic for the connection later probably anyway. I just really would have liked to keep it as simple as possible for now.


#4

That doesn’t seem very RESTful to me. It seems more like a leaky abstraction from a SQL database. If you had a graph database with first class relationships it would make sense, but that’s obviously not the case for the OP.