【发布时间】:2014-03-19 00:55:09
【问题描述】:
我的测验装置有一个名为 questions 的属性,它是相关问题的 id 数组。我想在名为 firstQ 的模型上创建一个计算属性,该属性返回问题数组中的第一个 id。最终,我想使用该 ID 并使用它链接到我创建的问题路线。
我不知道如何访问问题数组中的第一个 ID。这甚至是解决这个问题的最佳方法吗?请帮忙。
App.ApplicationAdapter = DS.FixtureAdapter.extend();
App.Quiz = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
questions: DS.hasMany('question', {async: true}),
firstQ: function() {
return this.get('questions')//where to go next?, on right track?;
}.property()
});
App.Question = DS.Model.extend({
question: DS.attr('string'),
quiz: DS.belongsTo('quiz', {async: true}),
answers: DS.attr('string')
});
//fixtures
App.Quiz.FIXTURES = [
{
id: 1,
name: 'The JavaScript Quiz',
description: 'take this quiz and find out if you\'re a super genious',
questions: [100,101]
}
];
App.Question.FIXTURES = [
{
id:100,
question: 'Inside which HTML element do we put the JavaScript',
quiz: 1,
answers: [
{ answer: 'alpha', weight: 0 },
{ answer: 'beta', weight: 5 }
]
}
]
【问题讨论】:
标签: javascript ember.js ember-data