Skip to content Skip to sidebar Skip to footer

In Ember.js How Do I Create A Computed Property That References First Item In Property Containing An Array

I have quiz fixtures that have a property called questions, which is an array of ids of associated questions. I want to create a computed property on the model called firstQ that r

Solution 1:

Okay, so I figured out how to to do this via the computed property on the model. Here's the updated code for the App.Quiz model:

App.Quiz = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  questions: DS.hasMany('question', {async: true}),
  firstQuestion: function() {
    returnthis.get('questions.firstObject');
  }.property('questions.firstObject')
});

Solution 2:

You can use the firstObject property of Ember Arrays to create a binding between firstQ and the first Id in questions array. Define your firstQ property as a binding like this

App.Quiz = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  questions: DS.hasMany('question', {async: true}),
  firstQBinding: 'questions.firstObject'
});

Post a Comment for "In Ember.js How Do I Create A Computed Property That References First Item In Property Containing An Array"