Giter Club home page Giter Club logo

Comments (11)

amorey avatar amorey commented on July 17, 2024 1

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.

amorey avatar amorey commented on July 17, 2024 1

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.

akrawchyk avatar akrawchyk commented on July 17, 2024

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.

amorey avatar amorey commented on July 17, 2024

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.

akrawchyk avatar akrawchyk commented on July 17, 2024

@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.

amorey avatar amorey commented on July 17, 2024

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.

akrawchyk avatar akrawchyk commented on July 17, 2024

@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.

amorey avatar amorey commented on July 17, 2024

@akrawchyk I just wanted to check in. Do you have any thoughts on the proposed isDefined() method?

from loadjs.

akrawchyk avatar akrawchyk commented on July 17, 2024

@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.

akrawchyk avatar akrawchyk commented on July 17, 2024

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.

amorey avatar amorey commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.