Comments (11)
Hi @akrawchyk - sorry for the late response. I have a working version with a new isDefined()
method:
<script>
if (!loadjs.isDefined('my-bundle')) {
loadjs(['1.js', '2.js'], 'my-bundle');
}
</script>
Let me know if this would solve your problem and if you have any suggestions for the method name and syntax.
from loadjs.
Thanks for the feedback! It would return true
after the bundle is registered. The .ready()
method can be used to execute a callback after the dependencies have been loaded.
from loadjs.
P.S. - what I'd really like to be able to do is use bundle id's in the paths array, like the following:
loadjs([
'/js/select2.js',
'/css/select2-theme.css'
], 'select2', {
success: initKeywordAutocomplete
});
loadjs([
'select2',
'/js/select2-places.js',
], 'select2-places', {
async: false, // load in series to ensure select2 is available before the places data adapter
success: initLocationAutocomplete
});
from loadjs.
Hi @akrawchyk!
You can use the .ready()
method to use bundle ids as dependencies. For example, you can load select2.js
and select2-places.js
sequentially like this:
loadjs(['/js/select2.js', '/css/select2-theme.css'], 'select2', {
success: initKeywordAutocomplete
});
loadjs.ready(['select2', 'select2-places'], {
success: function() {
loadjs('/js/select2-places.js', {
success: initLocationAutocomplete
});
}
});
If you want to load select2.js
and select2-places.js
in parallel and execute them in series this should work:
loadjs(['/js/select2.js', '/css/select2-theme.css'], 'select2', {
success: initKeywordAutocomplete
});
loadjs('/js/select2-places.js', 'select2-places', {async: false});
loadjs.ready(['select2', 'select2-places'], {
success: initLocationAutocomplete
});
However, given my understanding of the problem I think I would just load all the files with async: false
and run both init methods after load:
loadjs([
'/js/select2.js',
'/js/select2-places.js',
'/css/select2-theme.css'
],
{
async: false,
success: function() {
initLocationAutocomplete();
initKeywordAutocomplete();
}
}
);
Hope that helps!
from loadjs.
@amorey thanks for the speedy response and the helpful examples!
I've been tinkering with this and was overlooking the ready
function, I appreciate you pointing it out. This works well for a single script, and I have updated my code.
I might be spinning my wheels here, but what about detecting bundle loads across modules. For example, if 2 distinct modules that may or may not be loaded on the same page depend on select2:
<script>
loadjs([
'js/select2.js',
'css/select2-theme.css'
], 'select2');
</script>
<script>
try {
loadjs([
'js/select2.js',
'css/select2-theme.css'
], 'select2');
} catch (e) {
// another module registered loading select2
}
</script>
Is it possible to detect select2 being previously loaded without using try/catch or a global variable I set myself? If I were to assign a different bundle id to each loadjs call, the scripts would be added to the DOM twice...
edit: I know I can do a call to ready
to determine when a bundle loads, but in the above example that would assume another script is responsible for initiating the select2 bundle load. This doesn't work in this case since the modules can be used independently and both either need to ensure select2 is loaded, or load select2.
from loadjs.
Currently it's not possible to inspect LoadJS
's internal bundle cache without using try/except
as in your example. Would exposing the bundle ids via a dictionary work for you?
<script>
if (!loadjs.bundleIds['my-bundle']) {
loadjs(['1.js', '2.js'], 'my-bundle');
}
</script>
from loadjs.
@amorey that would be all I would need, but that does expose the id's to be modified externally. Changing or removing them could lead to unexpected behavior. Your call on what's best for loadjs! 😄
from loadjs.
@akrawchyk I just wanted to check in. Do you have any thoughts on the proposed isDefined()
method?
from loadjs.
@amorey this looks great! I think it's a good idea to avoid exposing the bundle registry as an object, so having an isDefined
API method makes sense to me 👍
from loadjs.
Literally just thought of this as soon as I pressed "Comment"...
When are you thinking the call to isDefined
would return true
for a bundle? After it's initially registered, or after it's been loaded? I'd think returning true
after a bundle is registered is a must, otherwise the same bundle could be registered twice while we wait for the bundle to load.
from loadjs.
Done! I added an isDefined()
method to v3.4.0 and pushed the code to github/npm. Let me know if you run into any problems using the new method.
from loadjs.
Related Issues (20)
- Announcement: New version with support for Promises and an Edge CSS onerror bugfix HOT 1
- Update comments in src to clarify arguments to loadJs HOT 1
- Custom script attributes HOT 3
- Ability to insert elements after or before certain element HOT 2
- Image loading and error handling HOT 2
- Support "defer" as well as "async"? HOT 6
- query args cause issues with native file type detection HOT 5
- Idempotent request option HOT 5
- Can not detect image if url has ?=rand123456 HOT 1
- Announcement: New version with support for image/css urls with query arguments and anchor tags
- Action required: Greenkeeper could not be activated 🚨
- How to Solve the Problem of Repeated Loading of Scripts HOT 3
- Is it possible to load JSON? HOT 2
- Stylesheets not loading in ie11 HOT 4
- Using loadjs in a Vanilla project: require is not defined error HOT 2
- CSS order HOT 2
- Security policy questions HOT 3
- Old browsers (Opera12) can't use document.head HOT 3
- Success/Error callbacks never called when the script element has the "nomodule" attribute. HOT 15
- More details than "path not found" HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loadjs.