dhruvaray / backbone-associations

Create object hierarchies with Backbone models; Respond to hierarchy changes using regular Backbone events.
http://dhruvaray.github.io/backbone-associations/
MIT License
492 stars 75 forks source link

Is it or will it be possible to make recursive relations? #125

Closed janispauls closed 10 years ago

janispauls commented 10 years ago

Example - http://jsfiddle.net/janispauls/PB62k/

jdkanani commented 10 years ago

Not automatically. But possible:

var model = new SomeModel();
model.set({childModels: [model]});
console.log(model.get('childModels'))
jdkanani commented 10 years ago

Full snippet would be:

var SomeModel = Backbone.AssociatedModel.extend({
    relations: [{
        type: Backbone.Many,
        key: 'childModels',
        relatedModel: function(){ // pass function or string
            return SomeModel;
        }
    }],
    defaults: {
        childModels: [],
        name: 'test'
    }
});

model = new SomeModel();
model.set({childModels: [model]});
console.log(model.get('childModels'));

Let me know if it helps.

janispauls commented 10 years ago

Thank you. I was trying to put 'relations' key as a function not 'relatedModel'. Seems it will work fine.

jdkanani commented 10 years ago

:thumbsup: