Alternative way to construct instances of classes in snippets?

43 views
Skip to first unread message

Galder Zamarreño

unread,
Jan 8, 2013, 1:45:32 PM1/8/13
to lif...@googlegroups.com
Hi all,

I have a snipper that contains something like this:

<form class="lift:authors.add?form=post">

Internally Lift will try to find an Authors class in the snippet/ folder and construct it with an empty constructor and then call add.

I was wondering if there's a way to configure Lift to construct Authors instances in a different way instead of the empty constructor.

Any ideas?

Cheers,
Galder

Jeppe Nejsum Madsen

unread,
Jan 8, 2013, 2:20:26 PM1/8/13
to lif...@googlegroups.com

Only when you use URL parameters, have a look at section 3.2.7: http://simply.liftweb.net/index-3.2.html#toc-Subsection-3.2.7

/Jeppe

Nolan Darilek

unread,
Jan 8, 2013, 2:30:58 PM1/8/13
to lif...@googlegroups.com
Alternatively, you might use a Screen rather than a snippet, then construct the class in finish(). I've moved almost entirely to immutable case classes for my modeling, and these get created in one call in the finish() method based on field values, with the occasional copy() for modifications.
--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 

David Pollak

unread,
Jan 8, 2013, 9:06:38 PM1/8/13
to lif...@googlegroups.com
How do you want to construct the instance?

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 



--
Telegram, Simply Beautiful CMS https://telegr.am
Lift, the simply functional web framework http://liftweb.net

Galder Zamarreño

unread,
Jan 9, 2013, 3:45:27 AM1/9/13
to lif...@googlegroups.com
To be precise, I want Authors to be a managed CDI bean so that I can inject stuff like @PersistenceContext.

For that to work, instead of constructing Actors via Reflection, I'd like to intercept this step to plug in my own way of creating instances selectively. IOW, some classes might not be managed, in which case I'd delegate to the standard Lift mechanism, but for those classes referenced from snippets that I know have some annotations, I'd like to create them via the CDI bean manager.

Cheers,

Andreas Joseph Krogh

unread,
Jan 9, 2013, 3:51:58 AM1/9/13
to lif...@googlegroups.com
På onsdag 09. januar 2013 kl. 09:45:27, skrev Galder Zamarreño <gal...@zamarreno.com>:
To be precise, I want Authors to be a managed CDI bean so that I can inject stuff like @PersistenceContext.

For that to work, instead of constructing Actors via Reflection, I'd like to intercept this step to plug in my own way of creating instances selectively. IOW, some classes might not be managed, in which case I'd delegate to the standard Lift mechanism, but for those classes referenced from snippets that I know have some annotations, I'd like to create them via the CDI bean manager.
 
I use AspectJ and AOP for injecting stuff into instances of classes I don't control the creation of. Even tho AOP is not the most popular mechanism on this list "it works for me". An example of this can be found here: https://github.com/andreak/on-example-rpm
 
--
Andreas Joseph Krogh <and...@officenet.no>      mob: +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
Public key: http://home.officenet.no/~andreak/public_key.asc
 

Jeppe Nejsum Madsen

unread,
Jan 9, 2013, 4:23:02 AM1/9/13
to lif...@googlegroups.com
On Wed, Jan 9, 2013 at 9:45 AM, Galder Zamarreño <gal...@zamarreno.com> wrote:
To be precise, I want Authors to be a managed CDI bean so that I can inject stuff like @PersistenceContext.

For that to work, instead of constructing Actors via Reflection, I'd like to intercept this step to plug in my own way of creating instances selectively. IOW, some classes might not be managed, in which case I'd delegate to the standard Lift mechanism, but for those classes referenced from snippets that I know have some annotations, I'd like to create them via the CDI bean manager.


You can always register your own snippets using LiftRules.snippetDispatch. It's slightly more involved, but you avoid reflection and can control the instantiation.

/Jeppe

Galder Zamarreño

unread,
Jan 9, 2013, 4:25:29 AM1/9/13
to lif...@googlegroups.com


On Wednesday, 9 January 2013 09:51:58 UTC+1, andreak wrote:
På onsdag 09. januar 2013 kl. 09:45:27, skrev Galder Zamarreño <gal...@zamarreno.com>:
To be precise, I want Authors to be a managed CDI bean so that I can inject stuff like @PersistenceContext.

For that to work, instead of constructing Actors via Reflection, I'd like to intercept this step to plug in my own way of creating instances selectively. IOW, some classes might not be managed, in which case I'd delegate to the standard Lift mechanism, but for those classes referenced from snippets that I know have some annotations, I'd like to create them via the CDI bean manager.
 
I use AspectJ and AOP for injecting stuff into instances of classes I don't control the creation of. Even tho AOP is not the most popular mechanism on this list "it works for me". An example of this can be found here: https://github.com/andreak/on-example-rpm

Sure, AOP is an option but using it gives me the creeps. It's not an easy technology to use, nor to debug. 

Thanks for the suggestion though :)

Galder Zamarreño

unread,
Jan 9, 2013, 4:30:49 AM1/9/13
to lif...@googlegroups.com
^ I'll check it out. 

Dunno the exact details about LiftRules.snippetDispatch, but the idea is that user's snippets and code remains the same... IOW, I would not want the user to do any different. What I am to do is to insert something in the middle that transparently creates the object according to the metadata info I have.

To get the full perspective of this, I'm doing this for Escalante, which is the Scala application server I'm building on top of JBoss AS7.

Anyway, thanks a lot for the suggestions Jeppe, I'll give it a shot! :)
 

/Jeppe

David Pollak

unread,
Jan 9, 2013, 11:06:28 AM1/9/13
to lif...@googlegroups.com
Keep in mind that LiftRules.snippetDispatch is something that takes a PartialFunction so you can do a whole lot of stuff dynamically if you implement your own partialfunction that looks to whatever classes you want and does that sort of stuff.

 

/Jeppe

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 

Galder Zamarreño

unread,
Jan 24, 2013, 1:35:32 PM1/24/13
to lif...@googlegroups.com
David, thanks for the extra info. I'm still trying to get my head around this. 

Let's take a concrete example and let me explain if the following could work. Imagine someone writes a snippet like this:

<tr class="lift:authors.list">
 
<td class="name">Name</td>
 
<td><a class="books"></a></td>
 
<td><a class="edit">Edit</a></td>
</tr>

Can I write a rule for LiftRules.snippetDispatch that captures not only "authors.list" but any other name that might be given, i.e. in pseudo code:

LiftRules.snippetDispatch.append {
 
case "*" =>
   
// take whatever is '*', figure out what the class name looked for is
   
// go to to bean manager and see if there's a bean for that class
   
// if no bean, present, act as Lift would normally do
   
// if bean present, call the method desired on the bean (i.e. list() in the above example)
...

Is something like this possible?

David Pollak

unread,
Jan 24, 2013, 3:11:14 PM1/24/13
to lif...@googlegroups.com
On Thu, Jan 24, 2013 at 10:35 AM, Galder Zamarreño <gal...@zamarreno.com> wrote:
David, thanks for the extra info. I'm still trying to get my head around this. 

Let's take a concrete example and let me explain if the following could work. Imagine someone writes a snippet like this:

<tr class="lift:authors.list">
 
<td class="name">Name</td>
 
<td><a class="books"></a></td>
 
<td><a class="edit">Edit</a></td>
</tr>

Can I write a rule for LiftRules.snippetDispatch that captures not only "authors.list" but any other name that might be given, i.e. in pseudo code:

LiftRules.snippetDispatch.append {
 
case "*" =>
   
// take whatever is '*', figure out what the class name looked for is
   
// go to to bean manager and see if there's a bean for that class
   
// if no bean, present, act as Lift would normally do
   
// if bean present, call the method desired on the bean (i.e. list() in the above example)
...

Is something like this possible?

Sure. Keep in mind that append takes a PartialFunction, so you don't need to do pattern matching, you just have to implement PartialFunction (isDefinedAt and apply) and do the logic in your PartialFunction.
Reply all
Reply to author
Forward
0 new messages