user29578
user29578

Reputation: 689

Share variable across domain and controller in grails

can i have one variable shared between two classes in grails, like in my controller i want to set a variable processStart as true, and once the after save method is done in my domain class of that controller i want to set it false like this,

class EmployeeController{
    def insert() {
         for (i in 1..params.numberOfEmp.toInteger()) {
           Employee emp = new Employee(params)
        processStart = true // set this variable here
        emp.save()
      }
       }
} 

and in domain class

class Employee {
   /** domain structure **/
def afterInsert () {
       processStart = false // and after this, set this variable here
  }
}

Upvotes: 1

Views: 287

Answers (1)

sean3838
sean3838

Reputation: 65

Try using a session variable, you shouldn't do this with a static variable.

class EmployeeController{
    def insert() {
        for (i in 1..params.numberOfEmp.toInteger()) {
            Employee emp = new Employee(params)
            session['processStart'] = true // set this variable here
            emp.save()
        }
    }
}

and in domain class:

class Employee {
    /** domain structure **/
    def afterInsert () {
        session['processStart'] = false // and after this, set this variable here
    }
}

Upvotes: 1

Related Questions