Created
August 17, 2012 14:38
-
-
Save eltimn/3379256 to your computer and use it in GitHub Desktop.
Get reference to an object via it's class name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Say you have some case objects like this: | |
package code | |
sealed trait Concept | |
case object Address extends Concept | |
case object City extends Concept | |
// From these you can create an html select list: | |
<select name="concept"> | |
<option value="code.Address$">Address</option> | |
<option value="code.City$">City</option> | |
</select> | |
// to get a reference to the case object on your backend server | |
import code.Concept | |
import net.liftweb.common.Box | |
import net.liftweb.http.S | |
import net.liftweb.util.Helpers.tryo | |
val concept: Box[Concept] = S.param("concept") | |
.flatMap(p => tryo(Class.forName(p).getField("MODULE$").get(null).asInstanceOf[Concept])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment