Skip to content

Instantly share code, notes, and snippets.

@stefanoverna
Created July 19, 2011 07:44
Show Gist options
  • Save stefanoverna/1091618 to your computer and use it in GitHub Desktop.
Save stefanoverna/1091618 to your computer and use it in GitHub Desktop.
App.Choice = SC.Record.extend({
label: SC.Record.attr(String, {
isRequired: YES
}),
question: SC.Record.toOne("App.Question", {
isMaster: NO
}),
isSelected: NO
});
App.Question = SC.Record.extend({
question: SC.Record.attr(String, {
isRequired: YES
}),
quiz: SC.Record.toOne("App.Quiz", {
isMaster: NO
}),
choices: SC.Record.toMany("App.Choice", {
inverse: 'question',
isMaster: YES
}),
correctAnswers: SC.Record.toMany("App.Choice", {
inverse: 'question',
isMaster: YES
})
});
App.Quiz = SC.Record.extend({
title: SC.Record.attr(String, {
isRequired: YES
}),
questions: SC.Record.toMany("App.Question", {
inverse: 'quiz',
isMaster: YES
})
});
App.quizController = SC.ArrayController.create({
currentIndex: null,
currentQuestion: (function() {
return this.get('content').objectAt(this.get('currentIndex'));
}).property('currentIndex', 'content'),
nextQuestion: function() {
return this.set('currentIndex', this.get('currentIndex') + 1);
},
previousQuestion: function() {
return this.set('currentIndex', this.get('currentIndex') - 1);
},
nextDisabled: function() {
return this.get('currentIndex') + 1 === this.getPath('length') || !this.getPath("App.currentQuestion.isFilled");
},
previousDisabled: function() {
return this.get('currentIndex') === 0;
},
progressString: (function() {
return "" + (this.get('currentIndex')) + "/" + (this.getPath('length'));
}).property('currentIndex')
});
App.currentQuestionController = SC.ObjectController.create({
contentBinding: 'App.quizController.currentQuestion'
});
App.currentChoicesController = SC.ArrayController.create({
contentBinding: 'App.currentQuestionController.choices',
allFilled: (function() {
console.log("111");
return this.get('content').filterProperty("isSelected", true).get('length') === this.getPath('correctAnswers.length');
}).property("@each.isSelected")
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment