I am not so sure how to implement a model which can share its state between several requests. I mean a model which holds a set of data not only a model which describes how the data looks like.
An example would be a quizz or a survey where the user steps through a couple of questions and each question is requested via an next or prev route in a subcontroller where the response or selected value of the user would be stored in a model and only at the end saved (to a db or a file).
I can inject the model to the UfrontJSApplication, and have then access in every route in the controller but then my api doesn't know anything about it. I can't store it inside the api because it's recreated on every new request.
The idea is like this:
class SurveyController extends Controller {
@inject public var surveyApi:app.api.AsyncSurveyApi;
@inject public var surveyModel:SurveyModel; // injected into UfrontJsApplication
@:route("/nextQuestion")
public function doNextQuestions(args: { index:Int } ) {
surveyModel.add(/*an item from the post vars*/);
return surveytApi.getNextQuestion(args.index) >> function(questionVO):ViewResult {
return new ViewResult(questionVO) );
};
}
//called after the last question via a button finish for example
@:route("/saveSurvey")
public function doSaveSurvey() {
//save the entire model filled up by every doNextQuestion route
return surveytApi.save(/*can't pass my model here*/) >> function(message):RedirectResult {
return new RedirectResult(message) );
};
}
}
Any thoughts about this? Maybe a solution should be implemented completely differently?
P.S. Perhaps someone with at least 1500 reputation can create an 'ufront' tag ?
A few options...
Use a UFHttpSession. You might need to create one that works client side, but it could just be a very simple Map<String,Dynamic>
. You could save the half-finished survey to the session in-between requests.
Save the model to the server API on each request, in it's half finished state.
If you're going to keep with the building of the survey client-side, before sending to the survey, you have to make the SurveyModel
persist between requests. You could do this using dependency injection like you have, or just by keeping it in a static variable that you can access between requests. Either way you should be able to access the model in your doSaveSurvey()
function, and send it to the API with surveyApi.save(mySurvey)
.
If there's an issue with that, it would be good to see some code for what SurveyModel
and SurveyApi
look like, and what the error message is.