Ajax adding onclick in HTML

231 views
Skip to first unread message

Roch Delsalle

unread,
Apr 30, 2012, 9:47:52 AM4/30/12
to lif...@googlegroups.com
Hi,

I recently started using Ajax with lift. I noticed that lift is adding onclick="" attributes into the HTML it produces.
I would like to know if lift is able to do it using Event Listeners ?

Thanks,

Roch

Diego Medina

unread,
Apr 30, 2012, 12:05:32 PM4/30/12
to lif...@googlegroups.com
I think you are looking for this
https://github.com/lift/framework/blob/master/web/webkit/src/main/scala/net/liftweb/http/SHtml.scala#L1116

Regards,

Diego
> --
> 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



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Roch Delsalle

unread,
Apr 30, 2012, 12:20:25 PM4/30/12
to lif...@googlegroups.com
Thank you very much

2012/4/30 Diego Medina <di...@fmpwizard.com>

Roch Delsalle

unread,
Apr 30, 2012, 12:40:34 PM4/30/12
to lif...@googlegroups.com
I have tried but it still adds the following code into my rendered html :

liftAjax.lift_ajaxHandler('F854894974127D1LRDR=' + encodeURIComponent(this.value), null, null, null)
Is it possible to bind it as an event ?

2012/4/30 Roch Delsalle <rdel...@gmail.com>

Diego Medina

unread,
Apr 30, 2012, 12:55:02 PM4/30/12
to lif...@googlegroups.com
did you check if it works?
It may be better if you could put together a sample application [1]
showing the problem and what you expect.
[1] https://www.assembla.com/wiki/show/liftweb/Posting_example_code

Regards,

Diego

Jeppe Nejsum Madsen

unread,
Apr 30, 2012, 4:16:45 PM4/30/12
to lif...@googlegroups.com
I assume you mean something like

element.addEventListener('click', function() { do ajax stuff }, false);

? If so, then no, not using the standard SHtml methods to generate the
HTML. But you can always construct your own event listeners and have
them perform the requested ajax call.

Out of curiosity, why would you rather use event listeners rather than
inline js?

/Jeppe

Roch Delsalle

unread,
Apr 30, 2012, 5:45:36 PM4/30/12
to lif...@googlegroups.com
Hi Jeppe,

Yes that's exactly what I want to do,
Well, the reason why I want to use event listeners is because I think it's cleaner.
I guess it doesn't make such a big difference but I was wondering why Lift doesn't do it by default.

Thanks for your help,

Roch

2012/4/30 Jeppe Nejsum Madsen <je...@ingolfs.dk>

Jeppe Nejsum Madsen

unread,
May 1, 2012, 4:07:31 AM5/1/12
to lif...@googlegroups.com
On Mon, Apr 30, 2012 at 11:45 PM, Roch Delsalle <rdel...@gmail.com> wrote:
> Hi Jeppe,
>
> Yes that's exactly what I want to do,
> Well, the reason why I want to use event listeners is because I think it's
> cleaner.
> I guess it doesn't make such a big difference but I was wondering why Lift
> doesn't do it by default.

I agree it's cleaner when you handcode the js/html but in the case of
Lift it would make the generation much more complicated (you seperate
the js from the html) with no real benefit.

The markup you write will still be clean as the inline js is handled
entirely by Lift and should not leak into your templates. Personally I
don't care how it looks on the browser (as long as it's fast :-) but
only how it looks during development

/Jeppe

Naftoli Gugenheim

unread,
May 1, 2012, 12:46:21 PM5/1/12
to lif...@googlegroups.com
You can use reactive-web's JsStub to write Javascript interfaces in Scala and call them from Scala code.



/Jeppe

Robert Marcano

unread,
May 8, 2012, 9:32:45 AM5/8/12
to lif...@googlegroups.com
When I do event listeners because of one of the following cases

1) When the same event is needed for many elements, for example for each
row of a table. Adding a listener to the container, table in my example,
and wait for the event to bubble from the row to the table. It is
cleaner than dozens on onclick attributes

2) When the client side JavaScript adds new elements that need events
captured, Using 1) you can add as many rows client side and the event
listener are already in place for each one

3) When I want automatic events added without need to add on* attributes
everywhere. See for example Twitter Bootstrap, you some data-*
attributes and the base installed event handlers are reused in all the page

How I do this? I create a <script> element for the page with JcCmds
server side

Function("loadCatalog",
List("id"),
SHtml.ajaxCall(JsVar("id"), Repository.load)._2)

This adds a function that I can later call from my client side added
event listeners

SHtml.onEvent reference by Diego is nice and I probably will use it now
for when I do not need to do client side processing of the event (for
example as for confirmation), but will use it when the server will
receive the event signal directly


>
> /Jeppe
>

Reply all
Reply to author
Forward
0 new messages