Question

I have a backbone application with the following architecture:

A PageGroupCollection is a collection of PageGroupModels.

A PageGroupModel has a PageCollection as one of it's attributes.

A PageCollection is a collection of PageModels


PageGroupModel has a permalink attribute

PageModel has a permalink attribute.

I need a function within PageModel that returns a permalink that includes both the permalink of PageModel thats PageCollection attribute contains PageGroupModel and also its own permalink attribute.

Something like the following:

getFullPermalink: function () {

    var parentPermanlink = this.owningCollection.modelCollectionIsAnAttributeOf.get('permalink');

    return parentPermalink + "/" + this.get('permalink');
}

Update: Worked by adding the following:

var pageGroup = pageGroupCollection.findWhere({ 'pageCollection': this.collection });
var pageGroupPermalink = pageGroup.get('permalink');
return pageGroupPermalink + "/" + this.get('permalink');
Was it helpful?

Solution

Backbone isn't really made to work with nested models (Backbone-Relational may be interesting). But if you have access to the entry point of your tree (in your example, to the instance of PageGroupCollection), you can do that:

var parent = myPageGroupCollection.findWhere({pageCollection: myPageModel.collection});

With myPageModel being your child model and pageCollection the collection attribute of the PageGroupModel.

Supposing you also don't mess with the collection attribute of your models (if your model is in several collections for example).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top