问题
So I've got two versions of this question, one a little simplified, one a bit more like what I'm trying to achieve.
- I have a "student" model and a "score" model. The "student" has many "scores". On the "student" controller, I'm trying to set a computed property "score" equal to a specified one of these "scores".
Is there some way I can pass in another argument (eg, so as return the first in the array)?
In controllers/student
score: function(){
return this.get('scores', 1);
}.property('scores')
An extra dimension here. The "score" model also belongs To an "objective" model. Can I set the "score" property on my "student" controller depending on the id of a chosen objective?
-------------------------------------------Update----------------------------------------------------------
I'm afraid I'm still stuck! I've been trying to figure out a little more but to no avail. I'll outline a little code to hopefully make my issue clearer.
My 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})
- So what I'd really like to do is in the "student" controller set the "score" property to the "scoreResult" integer, filtering by the value of the objective_id in the score model.
So elsewhere, I'll be able choose an objective, gets its objective_id, and then use this to set the student controller's "score" to that for the appropriate objective.
I hope this makes sense. I'm really struggling to find any tutorial/guidance for this, and struggling to figure it out for myself. I'd really appreciate any help.
回答1:
return this.get('scores.firstObject');
http://emberjs.com/api/classes/Ember.ArrayProxy.html
回答2:
To answer the question of extra arguments. You can add other arguments by using something like this
score : Ember.computed('first','second','third', function(){
//now use all three properties to do anything here
// you can use any number of properties
return this.get('model').filterBy('first',A);
});
Another syntax but with same meaning will be
score : function(){
}.property('first','second','third')
来源:https://stackoverflow.com/questions/28586899/ember-this-getone-from-an-array