You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
There are a lot of JS loaders, really. But none of them, at least within documentation, addresses a problem I'm eager to solve. That is to use single <script> line per whole project which inits page-based loading of dependencies.
Let's create a simple project with index, portfolio, and contacts pages.
Microframework will help us with routing:
$app->get('/portfolio/',
function () use ($app) {
$prices = array(
"price_mine" => "90",
"price_rival" => "120"
);
$app->render('portfolio.twig', $prices);
})->name('portfolio');
Template engine will help us with rendering:
{% extends "base.twig" %}
{% block content %}
<p>Aren't you sick and tired to pay {{price_rival}}…</p>.
{% endblock content %}
And the missing part in every page is <script src="/static/init.js"></script> that works as follows
all pages load jQuery from CDN or from my server as fallback,
index loads SoundManager to serve audio salutation,
portfolio loads Lightbox to serve images,
contacts loads Forms validator.
What's the best way to achieve it?
And how to do that with Lazyload whether it's possible?
Thank you in advance.
Warm regards from Russia, Alexander.
The text was updated successfully, but these errors were encountered:
I'm not quite sure I understand the question so I may be way off here.
If you mean how do you make sure you only load a single dependency once then....
I do it by simply storing an array of script urls and checking against them whenever a new request to load a script is made.
Something along the lines of this should work fine.
var scripts = [];
if(scripts.indexOf(url) === -1){
scripts.push(url); // Push the url into array before the complete callback so that we know instantly whether a load has been attempted.
LazyLoad.js(url, function(){
});
}
This way when navigating around my webapps each page, page fragment, dialog, whatever... has its own set of JS resources of which that particular piece of dynamically loaded content requires. My own mini framework attempts to load the correct resources whenever said content is loaded.
Each of these fragments may have the same dependencies. For instance:
Imagine a single page that has 3 dynamically loaded segments:
There are a lot of JS loaders, really. But none of them, at least within documentation, addresses a problem I'm eager to solve. That is to use single
<script>
line per whole project which inits page-based loading of dependencies.Let's create a simple project with index, portfolio, and contacts pages.
Microframework will help us with routing:
Template engine will help us with rendering:
And the missing part in every page is
<script src="/static/init.js"></script>
that works as followsWhat's the best way to achieve it?
And how to do that with Lazyload whether it's possible?
Thank you in advance.
Warm regards from Russia, Alexander.
The text was updated successfully, but these errors were encountered: