Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The JSON being returned is not what Backbone or Backbone-Relational is expecting by default.</p> <p>The expectation of Backbone and Backbone-Relational is:</p> <pre><code>{ "id": "123456", "Title":"Mr", "FirstName":"Joe", "Surname":"Bloggs", "Bookings": [ { "id": "585462542", "EventName": "Bla", "Location":"Dee Bla", "EventDate":"November 1, 2012" }, { "id": "585462543", "EventName": "Bla", "Location":"Dee Bla", "EventDate":"November 1, 2012" } ] } </code></pre> <p>To use your response, you need to create a <code>parse</code> function on the Client model that returns the structure I've posted above.</p> <p>A jsFiddle example of your model definitions working with my example JSON: <a href="http://jsfiddle.net/edwardmsmith/jVJHq/4/" rel="nofollow">http://jsfiddle.net/edwardmsmith/jVJHq/4/</a></p> <p><strong>Other notes</strong></p> <ul> <li>Backbone expects <code>ID</code> fields to be named "id" by default. To use another field as the <code>ID</code> for a model, use <a href="http://backbonejs.org/#Model-idAttribute" rel="nofollow">Model.idAttribute</a></li> <li>The "key" for the Bookings Collection I changed to "Bookings"</li> </ul> <p>Sample Code:</p> <pre><code>Client = Backbone.RelationalModel.extend({ urlRoot: '/URL-THAT-RETURNS-JSON/', relations: [ { type: Backbone.HasMany, key: 'Bookings', relatedModel: 'Booking', collectionType: 'BookingsCollection' } ], parse: function (response) { }, initialize: function (options) { console.log(this, 'Initialized'); } }); Booking = Backbone.RelationalModel.extend({ initialize: function (options) { console.log(this, 'Initialized'); } }); BookingsCollection = Backbone.Collection.extend({ model: Booking }); myClient = new Client( { "id": "123456", "Title":"Mr", "FirstName":"Joe", "Surname":"Bloggs", "Bookings": [ { "id": "585462542", "EventName": "Bla", "Location":"Dee Bla", "EventDate":"November 1, 2012" }, { "id": "585462543", "EventName": "Bla", "Location":"Dee Bla", "EventDate":"November 1, 2012" } ] }); console.log(myClient);​ </code></pre> <p><strong>Post Edit</strong></p> <p>Yes, the JSON pretty much defines the attributes of the model. You can use a combination of <a href="http://backbonejs.org/#Model-parse" rel="nofollow"><code>parse()</code></a>, <a href="http://backbonejs.org/#Model-defaults" rel="nofollow"><code>defaults</code></a>, and <a href="http://backbonejs.org/#Model-validate" rel="nofollow"><code>validate()</code></a> to better control what attributes are valid and allowed.</p> <p>The canonical way of reading and setting properties on a Backbone Model is through the <a href="http://backbonejs.org/#Model-get" rel="nofollow"><code>get()</code></a>, <a href="http://backbonejs.org/#Model-escape" rel="nofollow"><code>escape()</code></a>, and <a href="http://backbonejs.org/#Model-set" rel="nofollow"><code>set()</code></a> functions.</p> <p><code>set</code> is especially important as this does a bunch of housekeeping, such as validating the attribute and value against your <code>validate</code> function (if any), and triggering change events for the model that your views would be listening for.</p> <p>In the specific case of the situation in this answer, you might</p> <pre><code>myClient.get('Title'); // =&gt; "Mr" myClient.get('Bookings'); //=&gt; an instance of a BookingsCollection with 2 models. myClient.get('Bookings').first().get('Location'); //=&gt; "Dee Bla" myClient.get('Bookings').last().get('Location'); //=&gt; "Dee Bla" myClient.get('Bookings').first().set({location:"Bora Bora"}); myClient.get('Bookings').first().get('Location'); //=&gt; "Bora Bora" myClient.get('Bookings').last().get('Location'); //=&gt; "Dee Bla" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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