Backbone.js relationships

I am having a problem wrapping my head around relational models in Backbone. I just started using it and I am entrusted with a fairly large application.

The main problem I am facing is that I have a model that needs to contain a collection.

Here's what I need to work with:

  • Modela
    • id: _id
    • url: api / model /: modelA_id
    • nested:
      • url: api /: modelA_id / nest

I think I am doing this a bigger deal than I need to, but I just can't get my head around how to do it.

Any help would be greatly appreciated.

+3


source to share


1 answer


The most important thing to wrap your head with Backbone is how to use events correctly to resolve everything in your application. Another important thing to understand is that there are probably 5 different ways to attack a problem, where none is better / worse than the other.

Given the loose structure you provided, I would do something like:



var YourApp = {
   Models : {}
   Collections : {}
   Views : {}
};

YourApp.Models.First = Backbone.Model.extend({
  initialize : function(){
      var nestedCollection;
      this.url = 'api/model/' + this.id;
      nestedCollection = new Backbone.Collection({
        url : this.url + '/nest'
      });
      this.set('nested', nestedCollection);
    }
});

new YourApp.Models.First({
  id : 23
});

      

+9


source







All Articles