Single call for Jquery plugins?

Asked

Viewed 64 times

3

I was thinking of something for calling Jquery plugins, I would call all plugins in a single page, but loading them only if they are used.

For example:

On page X I use Datatables, then:

if($(".dataTable") != undefined){ //se a página usar o dataTable

    if($.dataTable == undefined){ //e se ele não foi carregado

        //carregaria os arquivos necessários
        $.getScript("dataTable.min.js", function(){
            $(".dataTable").dataTable(); //e então incluiria a chamada
        });

    }

}

This would be done for all plugins used throughout the system. This way, only the plugins used on the page would be loaded, and would not need to be including the files and the so-called page by page.

My doubts are:

  • That would be feasible?
  • Is there any way to do something like this?

The language used is PHP.

It’s just that I’m actually using the following Dashboard: http://demo.kimlabs.com/gentelella/production/, it provides several interesting plugins, would like to leave the call of all of them available but this would be TOO heavy for loading, and loading them only on the pages on which they were used would be a job (besides being horrible for maintenance).

  • Loading while running Javascript would be too slow (unviable). There is not a good model to follow and as a web developer I believe that this is some of the dilemmas of HTML development. Put in question your file structure and page calls, language and etc. So I can help with some ideas I have on the subject.

  • I left more information at the end of the question, thanks in advance.

  • @Mukotoshi I think it would be nice if you change the title to "Javascript library loading on demand".. just a suggestion :D

1 answer

4


When you are going to use a lot of Javascript code on a single page, it is important to think about the performance of your application, besides so that your application is prepared to grow is interesting your codes are organized.

That’s where the doubts begin!

I create several files and I keep calling outside, without worrying about the requisition amount?

I put all my codes into one file and make the minification of the same ?

The answer to the above questions is; It depends on your app’s need!

And how will you know what your need is? You will need to do performance tests :(

If you have the need to modularize the application, I propose to you the use of Requirejs. With it you will upload and parse your Javascript and CSS files in the background.

Requirejs uses a modular structure with Dependency Injection, so that the required modules are loaded asynchronously and on demand! Doing what you requested above. Very cool neh?? :D

You can find the Requirejs here, your documentation here and a tutorial/presentation in Portuguese here.

Obs: There are several other API’s that do almost the same thing as Requirejs, I indicated the same because I use in my projects :

Browser other questions tagged

You are not signed in. Login or sign up in order to post.