Ember - this.get(one from an array)

孤街浪徒 提交于 2019-12-13 05:52:42

问题


So I've got two versions of this question, one a little simplified, one a bit more like what I'm trying to achieve.

  1. 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')
  1. 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})
  1. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!