vendredi 20 février 2015

Accessing attributes of related models in the controller

I'm been asking a few questions around the same sort of lines and haven't managed to get an answer (perhaps I've been unclear), and just can't figure it out myself.


The quick version of the question is:


Can anybody shed some light on whether its possible to access a property of a related model from a controller?


Its a little tricky, so I'll try and explain the context.


I have the following models:


student



scores: DS.hasMany('score', {async: true}),
name: DS.attr('string')


objective



name: DS.attr('string'),
scores: DS.hasMany('score', {async : true})


score



scoreResult: DS.attr('number'),
objective: DS.belongsTo('objective', {async: true}),
student: DS.belongsTo('student', {async: true})


In my template, I can access attributes of related models with no problem. For example, something like this:



{{#each student in model}}
{{student.name}}
{{#each score in student.scores}}
{{score.objective.name}}
{{score.Result}}
{{/each}}
{{/each}}


What I ultimately want to do is create a "score" property on the student controller that loads the appropriate score/result when I choose an objective elsewhere. But I'm falling at the first hurdle.


Though I can access attributes of related models in the templates, I can't seem to in the controller. I'm expecting that I can do something like this in "student controller":



score: function(){
var selectedObjID = 5;
return this.get('model.scores').findBy('objective_id', 1).get('scoreResult');


I've tried every variation I can think of, including what I would have expected from reading the guides/api.


I feel like I'm missing something obvious - surely this should have been done enough before for there to be some documentation?


Its also important to do in the one route - I don't want details dealt with via another route.


Aucun commentaire:

Enregistrer un commentaire