我不太确定如何实现一个可以在多个请求之间共享其状态的模型。我的意思是一个包含一组数据的模型,而不仅仅是一个描述数据外观的模型。
一个示例是测验或调查,其中用户逐步完成几个问题,并且每个问题都通过子控制器中的下一个或上一个路由请求,其中用户的响应或选择的值将存储在模型中,并且仅在最后保存(到数据库或文件)。
我可以将模型注入 UfrontJSApplication,然后访问控制器中的每条路由,但我的 api 对此一无所知。我无法将它存储在 api 中,因为它会在每次新请求时重新创建。
这个想法是这样的:
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) );
};
}
}
对此有什么想法吗?也许应该以完全不同的方式实施解决方案?
PS 也许至少有 1500 声望的人可以创建一个 'ufront' 标签?