0

I'm messing around with backbone, marionnette, and wondering how I should be dealing with relational models. My main reason is i'd like to be able to use data in two different related models in the same template. e.g.

Thing = Backbone.Model.extend({defaults: {label: null, uri: null}});
Things = Backbone.Collection.extend({model: Thing});

Relationship = Backbone.Model.extend({defaults: {subject: null, predicate: null, object: null}});
Relationships = Backbone.Collection.extend({model: Relationship});

var things = new Things([
    new Thing({label: 'Sam', uri: 'AAAA'}),
    new Thing({label: 'is friends with', uri: 'BBBB'}),
    new Thing({label: 'Violet', uri: 'CCCC'}),
    new Thing({label: 'Fred', uri: 'DDDD'})
]);

var relationships = new Relationships([
    new Relationship({subject: "AAAA", predicate: "BBBB", object: "CCCC"}),
    new Relationship({subject: "AAAA", predicate: "BBBB", object: "DDDD"})
]);

So, the relationships array is just holding references to items in the Things array matching the uri attribute. What I want to do in the Relationship template is something like this (using dot notation to get at the associated Thing model data):

<script type="text/template" id="relationship-template">
    <td><%= subject.label %></td>
    <td><%= predicate.label %></td>
    <td><%= object.label %></td>
</script>

Any ideas the best way to handle this? I want to avoid duplicating things in memory.

Result should be:

Sam is friends with Violet

Sam is friends with Fred

I've looked at Backbone relational but haven't had any luck getting it working :(

This is what I tried:

Relationship = Backbone.RelationalModel.extend({
    relations: [
        {
            type: Backbone.HasOne,
            key: 'subject',
            keySource: 'uri',
            keyDestination: 'subjectObject',
            relatedModel: Thing
        }
    ]
});
Devin McQueeney
  • 1,277
  • 2
  • 14
  • 31

1 Answers1

0

Ah - i figured it out -

It's looking for an ID field to map whatever you have in key to - so I renamed uri to id and all is well. Too bad you can't explicitly tell it which field to map to.

Devin McQueeney
  • 1,277
  • 2
  • 14
  • 31