Francis-Lyon (3) [Avatar] Offline
We're using this textbook for our Mobile Health Apps class. The app we’re developing is used for performing child assessments on an android tablet by researchers in Kenya. Questions for each assessment, along with question ID and input type (ex: radio, text) are read from a plain text file so researchers can add or modify questions. Answers are collected from the view and propagated back to the model (factory service). This allows the assessment results to be saved from either the view for that assessment or from the export page.

As we add different assessments we realize that something like an abstract factory can encapsulate the basic functionality of reading a question file and propagating answers back to the model, and be reused for each assessment. However, we're not sure on best practices to do this. Alternatively, it might be more flexible to read in all the question files contained in one directory and generate a view for each of these. This would give the users more flexibility to add and subtract assessments themselves, not only modify an existing assessment. This sort of factory might create a model that is an array of assessments, each of which contains the functionality specified above for reading in questions from a file and saving questions back to the model.

Code for a single factory (as a service) and a controller is pasted below, but we should have a solution that accommodates 10 of these assessments while coding in one place. What would be best practice? Should we create the models for the 10 assessments via inheritance or use one factory to create a model that is an array of however many assessments we find in the directory? Or should we do something entirely different?

Thanks for your advice!

.controller('PaperCtrl', function($scope, $state, Paperpenservice) {
$scope.questions = Paperpenservice.getQuestions();

$scope.setanswers = function(qarray) {

ap.factory('Paperpenservice', function ($http) {

function Paperpenservice () {
var self = this;
self.questions = [];
self.PAPER_PEN_PATH = '/locales/android/data/questions/paper-pen.txt'; // ***

self.getQuestions =function () {
$http.get(self.PAPER_PEN_PATH).success(function (result) {
var questionStrs = result.split(/\r\n/);

for (count = 0; count < questionStrs.length; count++) {
if (questionStrs[count]) {

var parts = questionStrs[count].split('|');
name: parts[0],
question: parts[1],
type: parts[2],
ans: 'No',
i: count

return self.questions;
self.setanswers = function (qarray){
for (index = 0; index < qarray.length; index++ ){
self.questions[index].ans = qarray[index].ans;

return new Paperpenservice();

// *** The above path for reading questions will change over the next few days to reading from a file in the externalDataDirectory so users can modify and add questions
jeremy.wilken (208) [Avatar] Offline

You might consider something like angular-formly which provides a lot of rich form functionality based on a JSON object definition and you could use that to help you scale more.

Its hard to give too much concrete advice here, but in general I favor abstracting code when you start to feel the pain of your current implementation. It sounds like you are there, so I would want to have a backend database that stores the definition of what a form contains (its fields and descriptions) and the results based on that form. Then you write the service that knows which of the form definitions to load, and how to store those results into the database.

In one particular application I built, we had a catalog of forms that would be used depending on the user demographic and it was often updated, and this is the basic approach I took to give the most flexibility.

Francis-Lyon (3) [Avatar] Offline
Thanks so much, Jeremy - it's great to know about angular_formly.