I have an application controller which has 2 methods. In the first method I access database and retrieve a list of drop-down elements for the form. I store the retrieved elements in class level variables. In the second method if there is an error it displays the form as it is else it shows the result. The problem is I class level variables return empty.
class Application extends Controller {
var appList = Seq[AppDetail]()
var countryList = Seq[Country]()
def home(page:Int,filter: String): Action[AnyContent] = Action.async { implicit request =>
val futures = for {
appDetails <- AppDetailsDAO.listAll() //DB retrieve
countries <- CountriesDAO.listAll() //DB retrieve
} yield (appDetails, countries)
futures.map{case (appDetails, countries) => {
appList = appDetails
countryList = countries
Ok(appList, countryList)
}
}
def result(page:Int, filter: String): Action[AnyContent] = Action.async { implicit request =>
analyticForm.bindFromRequest.fold(
formWithErrors => {
Future.successful(BadRequest(html.home(formWithErrors, appList, countryList)) //appList, countryList returns empty
},
analyticData => {
Ok(appList, countryList) //appList, countryList returns empty
}
}
}