So I have this dilema, and its probably just my lack of understanding of how to solve problems like this in Ember. I am using Ember data, with fixtures for now, and I have a complex object relationship. My question is, how do I get data from child objects?
For example, I have some activities, and these activities have relations to other objects in them, which I have set up the relationships in the Data model, but I cannot figure out how to access them since a route only returns a “model” which is essentially one data type. Here is some code which I hope will be a good illustration of my problem:
App.Activity = DS.Model.extend({
contact: DS.belongsTo('contact'),
user: DS.belongsTo('user'),
type: DS.attr('number'),
subject: DS.attr('string'),
contact_id: DS.attr('number'),
dueDate: DS.attr('Date'),
assignedUser: DS.attr('number'),
completed: DS.attr('boolean'),
notes: DS.attr('string')
});
App.Contact = DS.Model.extend({
activity: DS.belongsTo('activity'),
firstname: DS.attr('string'),
lastname: DS.attr('string'),
email: DS.attr('string'),
phone: DS.attr('string'),
mobile: DS.attr('string'),
organization_id: DS.attr('number'),
department: DS.attr('string'),
title: DS.attr('string'),
address: DS.attr('string'),
city: DS.attr('string'),
state: DS.attr('string'),
zip: DS.attr('string'),
notes: DS.attr('string')
});
So my activities have relations into contact and organizations. How do I access those objects from my handlebars template?
<script type="text/x-handlebars" id="activities">
<p>Activities</p>
<table>
<tr>
<th>Type</th>
<th>Due Date</th>
<th>Subject</th>
<th>Contact</th>
<th>Done</th>
</tr>
{{#each item in model}}
<tr>
<td>{{type}}</td>
<td>{{dueDate}}</td>
<td>{{#link-to 'activity' this}} {{subject}} {{/link-to}}</td>
<td>{{contact_id}}</td>
<td>{{input type="checkbox" name="completed" checked=completed}}</td>
</table>
{{/each}}
</script>
I want to displace the contact details here and not just the ID!
Similarly:
<script type="text/x-handlebars" id="activity">
<p>Activity</p>
<table>
<tr>
<th>Type</th>
<th>Due Date</th>
<th>Subject</th>
<th>Contact</th>
<th>Done</th>
</tr>
<tr>
<td>{{type}}</td>
<td>{{dueDate}}</td>
<td>{{subject}}</td>
<td>{{contact.firstname}}</td>
<td>{{input type="checkbox" name="completed" checked=completed}}</td>
</table>
</script>
I want to display a great deal of contact information here but I cant seem to get at the contact object!
App.ActivityRoute = Ember.Route.extend({
model: function() {
return this.store.find('activity');
}
});
Do I return contacts here somehow? Not sure how to deal with complex object relations in handlebars templates…