Reputation: 7693
assuming i have a filter (which is being invoked before every action), how can I pass an object, which is fetched from the database in it to every view? I do not want to refactor all my methods.
To vizualize:
I have a filter that sets an object, for example:
def god = God.find(1)
and I have 100 controllers, for each controllers 100 methods, which is 100 * 100 views. Usually in the action you write something like :
def index = {
def something = Something.find(1)
[something: something]
}
the question is how can I pass God
object as well, without having to modify the return array to
[something: something, god: god]
Upvotes: 2
Views: 1062
Reputation: 2243
other place where you can do the same thing is afterInterceptor. Here is the documentation for the same. afterInterceptor is best suited when you have some business logic for extracting the object and don't want to copy the same logic to filters.
Upvotes: 1
Reputation: 19702
You would want an after
interceptor type to your filter for that. after
takes model
as an argument to the closure, and you can add god to that model.
myFilter(controller:'*', action:'*') {
after = { model ->
def god = God.find(1)
model.god = god
}
}
Here's the relevant docs section.
Upvotes: 3