The template html is requested but is not injected into the body only
after someone actually opens a Tip (see Tips in -more and the show
method, my class is like an extended Tips). So the js execution with
evalScripts doesn't do anything (as it can't find the elements in the
dom yet for example).

Most javascript is now added/handled in the main page (event
delegation for catching buttons/link hrefs etc.), other scripts, like
start a slideshow in the tip when it opens, is stored on the tip
container and executed with Browser.exec right after it's injected
into the body.

I think it's a clean solution, but I'm not 100% sure about sending js
along html in the request response..

On Dec 30, 7:15 pm, Sean McArthur <[email protected]> wrote:
> Assuming the code is kind of short, I would keep the JS in your main file,
> and have it execute after the template request has completed.
>
> That being said, I have in past made a site that loaded every single page
> though Request, and if individual pages needed their own JS, I'd let them
> have it. I would query for all the script tags before insert the response,
> and append each script tag to the head/body. However, there is an option in
> Request.HTML called evalScripts, which should execute your javascript
> onComplete.
>
> http://mootools.net/docs/core/Request/Request.HTML
>
>
>
> On Thu, Dec 30, 2010 at 4:09 AM, Rolf -nl <[email protected]> wrote:
> > hmm, after typing I thought it would be silly to have "specific-tip-
> > close-js-code" in the html template. the html template used for the
> > tip has no direct relation with the tip (it could be used on another
> > place as well)...
>
> > still valid questions:
> > - Should I inject the basic Tip html structure into the body straight
> > away to allow auto exec. of the js code from the ajax response. Or
> > should I store any js coming from the response and execute that when
> > the Tip is injected into the body (the first time a trigger for a Tip
> > is clicked)...
> > - How dirty is js code in html requests in general? or is it normal
> > practice...
>
> > On Dec 30, 12:55 pm, Rolf -nl <[email protected]> wrote:
> > > Hi all,
>
> > > I was copying/changing the Tips class a bit so I can use it in a
> > > project.
> > > Basically I load in an html template structure that will be used to
> > > fill Tip data in various spots. For example, the template has a dl
> > > list ready with dt's and the dd's are filled when the Tip renders.
>
> > > The template has some buttons as well, like a close button and some
> > > app specific ones.
> > > To make the links work correctly in the Tip (= ajaxifying links or
> > > getting a working close button) I have to run some javascript like $
> > > ('btnClose').addEvent('click, function(e){ tip.hide(); });
>
> > > The thing is, the tip html is not injected into the page straight
> > > away, therefore any js in the response (from loading the template)
> > > executed in the Request doesn't work.
>
> > > Now I can do a couple of things and I wonder what the best (preferred)
> > > option would be:
> > > 1. No JS in html requests! It's always ugly. Just plain html and add
> > > all the extra's in the app later. This would mean using the fireEvents
> > > from the Tips class.
> > > 2. Send JS with the html request, it's the logical place. If there is
> > > any JS in the response then store the JS code on the element inside
> > > the Tips class. Then when it is injected into the body check if
> > > there's JS code stored and run Browser.exec on it.
> > > 3. Send the JS with the html request. Don't wait to insert into the
> > > body when the first Tip is "opened" but insert straight away so the js
> > > is executed immediately.
>
> > > What would you do?

Reply via email to