I'm using LOAD functionality to break my webpage into bite-sized "cards" or
"widgets" each of which is loaded separately when the page is called. My
overall website looks much like a "dashboard" with several of these widgets
on a single page.
I'm trying to understand the LOAD process and how I can control the loading
of Javascript snippets along with the HTML of the widget. The Javascript
added relies on other JS libraries already loaded -- only they aren't. Yet.
My regular Javascript libraries (including jQuery, Datatables, etc.) are
loaded at the end of the process, as usually recommended for better loading
performance. But the scripts loaded by LOAD are loaded before any of the
standard Javascript libraries. This causes JS idioms like "$(document)" to
fail because "$" isn't defined yet.
I've created a thread-safe var called "scripts" and made it a list. I
added JS scripts to this list inside the LOAD template:
{{=body}}
{{
xtra = current.scripts
if isinstance(script,list):
xtra.extend(xtra)
else:
xtra.append(xtra)
pass
}}
(Apologies on the look of the fragment but we seem to have lost
code-formatting ability in the latest "improvement" to Google Groups.)
Then at the end of my layout, I try to render these blocks at the end of
the main view in layout.html:
<!-- Extra scripts added at the end -->
{{block extra_scripts}}
{{
for ea in current.scripts or []:
=ea
pass
}}
{{end extra_scripts}}
Surprisingly, this does not work as expected. The page is rendered with
all widgets and each Javascript segment is rendered along with the HTML of
the widget -- NOT at the bottom of the page as commanded by the layout.html.
I'm mystified at how this occurs. I could understand if layout.html is
used at every invocation of LOAD, but that makes no sense whatever. How
would it "know" to skip all parts of layout.html except the script
rendering part at the bottom?
If there's a better solution to my conundrum I'd like to know about it.
The goal again is to defer the scripts from a LOAD component until AFTER
the main Javascript libraries are loaded. I can't use response.files
because these snippets are not *.js files, they are inline scripts. They
can't really be *.js files because they are dynamic and include
customization for each widget individually. So how to defer the scripts
until after the response.files have been written?
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/4bd7d370-84bb-4d2c-9773-e7621cd0e4d8n%40googlegroups.com.