Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone-Relational related models not being created
    text
    copied!<p>I'm trying to create a nested, relational backbone project but I'm really struggling. The rough idea of what I'm trying to do is shown below but I was under the impression upon calling fetch() on Client, a number of bookings would automatically be created based on the bookings being returned as JSON.</p> <p>The format of my JSON can be seen beneath the outline of the MVC:</p> <pre><code>/**************************************************** /* CLIENT MODEL - Logically the top of the tree /* has a BookingsCollection containing numerous Booking(s) /* Client /* -Bookings [BookingsCollection] /* -Booking [Booking] /* -Booking [Booking] /*****************************************************/ var Client = Backbone.RelationalModel.extend({ urlRoot: '/URL-THAT-RETURNS-JSON/', relations: [ { type: Backbone.HasMany, key: 'Booking', relatedModel: 'Booking', collectionType: 'BookingsCollection' } ], parse: function (response) { }, initialize: function (options) { console.log(this, 'Initialized'); } }); var Booking = Backbone.RelationalModel.extend({ initialize: function (options) { console.log(this, 'Initialized'); } }); var BookingsCollection = Backbone.Collection.extend({ model: Booking }); </code></pre> <p><img src="https://i.stack.imgur.com/f3owB.jpg" alt="Approximate JSON being returned"></p> <p>Any help outlining what I'm doing wrong would be massively appreciated.</p> <p>Thanks</p> <p>EDIT</p> <p>Thanks for taking the time to post the feedback, it's exactly what I was hoping for.</p> <p>Is it the case that the JSON physically defines the actual attributes of models if you don't go to the effort of setting attributes manually? In other words, if the JSON I get back is as you have suggested above, would Backbone simply create a Client object (with the 4 attributes id, title, firstname &amp; surname) as well as 2 Booking objects (each with 4 attributes and presumably each members of the BookingsCollection)?</p> <p>If this is the case, what is the format for referencing the attributes of each object? When I set up a non-backbone-relational mini-app, I ended up in a situation whereby I could just reference the attributes using Client.Attribute or Booking[0].EventDate for example. I don't seem to be able to do this with the format you have outlined above.</p> <p>Thanks again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload