Skip to content

Instantly share code, notes, and snippets.

@nicksteffens
Last active April 26, 2016 19:38
Show Gist options
  • Save nicksteffens/56a86a679e4b062341cb79a05105f110 to your computer and use it in GitHub Desktop.
Save nicksteffens/56a86a679e4b062341cb79a05105f110 to your computer and use it in GitHub Desktop.
This is a work around for creating a survey with dynamic data
// irkResults must be passed in to have access to the service
.controller('SurveyCtrl', ['config', '$scope', '$ionicModal', '$http', 'irkResults', function(config, $scope, $ionicModal, $http, irkResults) {
$scope.takingSurvey = false;
$scope.surveyError = false;
$scope.openModal = function() {
$scope.takingSurvey = true;
getQuestions();
};
$scope.closeModal = function() {
$scope.takingSurvey = false;
console.log('get results', irkResults.getResults());
$scope.modal.remove();
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// get the questions
function getQuestions() {
$http({
method: 'GET',
url: config.api.questions
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
$scope.modal = $ionicModal.fromTemplate(renderSurvey(response.data.questions), {
scope: $scope,
animation: 'slide-in-up'
});
$scope.modal.show();
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
$scope.takingSurvey = false;
$scope.surveyError = true;
console.log('questions error', response);
});
}
function renderSurvey(questions) {
return '<ion-modal-view class="irk-modal">'+
'<irk-ordered-tasks>'+
renderQuestions(questions)+
'</irk-ordered-tasks>'+
'</ion-modal-view>';
}
function renderQuestions(questions) {
var questionsArray = [];
for(i = 0; i < questions.length; i++ ) {
var questionType = questions[i].q_type;
switch (questionType) {
case 'boolean':
questionsArray.push('<irk-task><irk-boolean-question-step id="q'+questions[i].id+'" title="'+questions[i].question+'" text="Additional text can go here." true-text="Yes" false-text="No" /></irk-task>');
break;
case 'scale':
questionsArray.push('<irk-task><irk-scale-question-step id="q'+questions[i].id+'" title="'+questions[i].question+'" text="1 being Never &amp; 5 Almost Always" min="1" max="5" step="1" value="3" /></irk-task>');
break;
}
}
return questionsArray.join('\n');
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment