Skip to content

Instantly share code, notes, and snippets.

@mauritslamers
Forked from stefanoverna/gist:1091618
Created July 19, 2011 08:59
Show Gist options
  • Save mauritslamers/1091773 to your computer and use it in GitHub Desktop.
Save mauritslamers/1091773 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 })
});
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"),
correctAnswers: SC.Record.toMany("App.Choice")
});
App.Quiz = SC.Record.extend({
title: SC.Record.attr(String, { isRequired: YES }),
questions: SC.Record.toMany("App.Question")
});
App.quizController = SC.ArrayController.create({
// do the next and previous in the state chart by getting the next object and do App.quizController.selectObject(obj)
// (or something similar, look up the code for this..
currentIndex: function(){
var selection,selObj;
selection = this.get('selection');
if(selection){
selObj = selection.get('firstObject');
return this.indexOf(selObj);
}
else return -1;
}.property('selection'),
nextIsEnabled: function(){
var index = this.get('currentIndex');
if((index !== -1) && (index < this.get('length')) return true;
}.property('selection'),
previousIsEnabled: function(){
//do here something similar as above but then check for 0
}.property('selection'),
progress: function(){
var index = this.get('currentIndex');
if(index !== -1) return index + "/" + this.get('length');
}.property('selection')
});
App.currentQuestionController = SC.ObjectController.create({
contentBinding: SC.Binding.single('App.quizController.selection')
});
App.currentChoicesController = SC.ArrayController.create({
contentBinding: 'App.currentQuestionController.choices',
filledAmount: function(){
return this.getEach('isSelected').get('length');
}.property('@each.isSelected')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment