If I have a page that generates search results based on some ajax components and a call to a controller method, what is the best way to save the state of that page (with the search results) so that I can return at any point in the session to see that page again?
1) Search, get results
2) Make some changes on another page
3) Return to search results, see same page as 1 left with.
I’ve read about web flows (which feels unneccessary and more formal than what I need) and AJAX save states, but also seems wrong. Also saw that Grails doesn’t come out of the box with this functionality (which seems weird for a web framework).
Edit:
How do I keep that searchResults list, which is populated by a call form my controller, for that session? I think I’m just missing that bridge between my logic and the session save.
class SearchService {
boolean transaction = false
static scope = 'session'
def searchResults
private Contact[] updateContactSearchList(String ns, Company c, String si, String res) {
def nameSearch = ns
def company = Company.get(c?.id)
def showInactives = si
def reset = res
if(res == "true") {
render(template:'searchResults', model: [searchResults:"", total: 0])
return
}
if(showInactives == "on" && nameSearch == "" && company != null) {
searchResults = Contact.withCriteria {
eq('company', company)
and {
eq('isActive', false)
}
}
searchResults.sort{it.lastName}
return searchResults
}
else if(showInactives == "on" && nameSearch == "" && company == null) {
searchResults = Contact.withCriteria {
eq('isActive', false)
}
searchResults.sort{it.lastName}
return searchResults
}
else if(showInactives == "on" && nameSearch != "" && company != null) {
searchResults = Contact.withCriteria {
eq('company', company)
and {
eq('isActive', false)
}
or {
ilike('firstName', '%' + nameSearch + '%')
ilike('lastName', '%' + nameSearch + '%')
ilike('fullName', '%' + nameSearch + '%')
}
}
searchResults.sort{it.lastName}
return searchResults
}
else if(showInactives == "on" && nameSearch != "" && company == null) {
searchResults = Contact.withCriteria {
eq('isActive', false)
or {
ilike('firstName', '%' + nameSearch + '%')
ilike('lastName', '%' + nameSearch + '%')
ilike('fullName', '%' + nameSearch + '%')
}
}
searchResults.sort{it.lastName}
return searchResults
}
else if(showInactives == null && nameSearch == "" && company != null) {
searchResults = Contact.withCriteria {
eq('isActive', true)
and {
eq('company', company)
}
}
searchResults.sort{it.lastName}
return searchResults
}
else if(showInactives == null && nameSearch == "" && company == null) {
searchResults = Contact.withCriteria {
eq('isActive', true)
}
searchResults.sort{it.lastName}
return searchResults
}
else if(showInactives == null && nameSearch != "" && company != null) {
searchResults = Contact.withCriteria {
eq('isActive', true)
and {
eq('company', company)
}
or {
ilike('firstName', '%' + nameSearch + '%')
ilike('lastName', '%' + nameSearch + '%')
ilike('fullName', '%' + nameSearch + '%')
}
}
searchResults.sort{it.lastName}
return searchResults
}
else if(showInactives == null && nameSearch != "" && company == null) {
searchResults = Contact.withCriteria {
eq('isActive', true)
or {
ilike('firstName', '%' + nameSearch + '%')
ilike('lastName', '%' + nameSearch + '%')
ilike('fullName', '%' + nameSearch + '%')
}
}
searchResults.sort{it.lastName}
return searchResults
}
else {
//log error
}
}
}
I would recommend creating a session scoped service, e.g:
this will handle the requests to the search engine and store them in a “results” object. Then, the javascript of the web components should access and render this object directly, without any need to resubmit the query – provided that you want the same results.