borismoore / jsdefer Goto Github PK
View Code? Open in Web Editor NEWScript loader, using the 'Deferred' pattern for async operations.
License: MIT License
Script loader, using the 'Deferred' pattern for async operations.
License: MIT License
I'm also seeing an issue where it appears to only actually load the last
I'm attempting to replace the old script loader from codeplex with this.
Add support for including css links in the loader... Just for convenience of using one thing to include everything, and it is only a few more lines of code.
When loading jQuery with jsdefer, $ becomes jQuery and loses all jsDefer methods/properties.
Trying to lazy load jQuery is throwing 4 script errors around line 414 of jsdefer.js.
I included jsdefer.js in the head, put in a def for jQuery in another script include under it as such:
var GoogleCDN = "//ajax.googleapis.com/ajax/libs";
var MicrosoftCDN = "//ajax.aspnetcdn.com/ajax";
var jQueryRelease = GoogleCDN + "/jquery/1.5.1/jquery.min.js"; //MicrosoftCDN + "/jquery/jquery-1.5.1.min.js";
var jQueryDebug = GoogleCDN + "/jquery/1.5.1/jquery.js"; //MicrosoftCDN + "/jquery/jquery-1.5.1.js";
jQueryRelease = (proto === "https" ? jQueryRelease : "//code.jquery.com/jquery-latest.min.js");
jQueryDebug = (proto === "https" ? jQueryDebug : "//code.jquery.com/jquery-latest.js");
$.deferDef({
jQuery: {
bare: true,
minUrl: jQueryRelease,
url: jQueryDebug
// OLD LOAD CHECK = isLoaded: !!(window.jQuery && jQuery.ui)
}
});
When a script is loaded on the page via script tag and then jsdefer tries to include it jsdefer should test for it.
jsdefer should provide a boolean expression test configurable in the script definition via deferDef... The old Sys.loader had this feature and it allows conditionally including a script based on how you define it. There are lots of uses for this...
Load script B only if script A is there (assuming you're not using jsdefer exclusively for script loading (we can't totally enforce it in some of our scenarios).
I have an array of modules names which I convert to an array of script URLs and a single callback to execute once they are all successfully loaded. Unless I have overlooked something, I have not seen a convenient way to execute this use case.
jsDefer fails when placed on a page with jQuery 1.4 and lower. Version check should be built into jsdefer so it doesn't try to use jQuery methods that aren't there.
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);
});
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
What's the best way to achieve it?
And how to do that with jsDefer whether it's possible?
Thank you in advance.
Warm regards from Russia, Alexander.
I happened across this post about chaining when().done() calls... It appears that it async loads everything and then chains the done methods together guaranteeing that they run in order. This seems more natural than having to nest a when() method inside a done() method to get this behavior. I might suggest adding this to jsdefer, or submitting to jQuery itself for inclusion...
http://www.bennadel.com/blog/2129-Experimenting-With-Sub-And-Deferred-Objects-In-jQuery-1-5.htm
This may seem like a helper, but the :// search on line 261 should just be // because you then don't have to worry about whether it's ssl or not and can pass in a script path like this:
//www.mysite.com/scripts/whatever.js
and it will use http or https depending on what mode the page is in. if you force urls to have http: or https: in them then we have to do an unnecessary script check for ssl and provide an alternate url.
that may be an option to add to this... I might suggest the following:
minurl = local min url on your domain
url = local url on your domain
cdnminurl = CDN min url
cdnurl = CDN url
and then add ssl to all those for ssl versions. unless you want to add the ssl check in the loader itself and have it change the script urls appropriately...?
Multiple script definitions with same url will choose last definition regardless of name.
Example:
jQuery: {
bare: true,
url: aspnetcdn + "/jQuery/jquery-1.7.1.js",
urlMin: aspnetcdn + "/jQuery/jquery-1.7.1.min.js",
loaded: "!!window.jQuery"
},
jQuery171: {
bare: true,
url: aspnetcdn + "/jQuery/jquery-1.7.1.js",
urlMin: aspnetcdn + "/jQuery/jquery-1.7.1.min.js",
loaded: "window.checkjQueryVersion(171)"
},
defer call to jQuery will load jQuery171 definition script. This is not expected (loaded was different, causing test to fail in some cases, which was a bad thing)
Hello !
What about the license ? In which conditions can I use it ?
Thanks !
When including jsdefer.js on a page, if it is already loaded it should not load again or lose its references to what scripts are loaded already.
Any plans to eventually implement the Asynchronous Module Definition (AMD) API?
In some cases, urls converted to lower case fail. The Azure CDN is case sensitive. This is hypo-critical. If you're going to build a case sensitive CDN you should build a loader that is case sensitive too. I'd rather have the CDN fixed but since that isn't likely to happen, the loader needs to play nice.
an example :
i have library file containing
window.$deferRun(
function( $, options) {
function A_processed() {
// ...
}
function B_processed() {
// ...
}
// here is the important point(!) :
// returning an deferred has no effect right now
return $.when( A_processed(), b_processed());
},
{
depends : [ 'ampere_core', 'ampere_util', 'ampere_module', 'ampere_action', 'ampere_history', 'ampere_state', 'ampere_transition', 'ampere_view', 'ampere_theme']
});
i want my library to be resolved
i did not find a way to return a deferred from my library function which is taken into account for its "resolved" state.
any idea how to do this ?
kind regards,
lars
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.