Looking For A Better Way To Dynamically Load Javascript For App Style Content










0















I'm building a web control panel. The short explanation of how it works: An application is installed onto the server which then delivers all app content to the frontend.



Each application uses Smarty for layout but should also be able to define styles and scripts for dynamic content (start content is loaded in by php).



The backend in php simply provides an array of these apps with their name, author, version, html content, icons, css files and js files. This is all stored in js classes and the scripts are loaded at the end with jquery's getscript function.



The problem with these scripts is that it's kind of a mess to get them all working side by side.



Let's say I have a settings app with multiple tabs, the javascript could contain something like this for the tab switching:



var elems;
var divs;
function init()
elems = document.getElementsByClassName("settings-item");
divs = document.getElementsByClassName("settings-item-content");
elems[0].classList.add("is-active");
divs[0].style.display = "block";

for(let i = 0; i < elems.length; i++)
elems[i].addEventListener("click", onItemClick);




function onItemClick(event)
for(let i = 0; i < elems.length; i++)
elems[i].classList.remove("is-active");


event.target.classList.add("is-active");

let id = event.target.id + "-content";

for(let i = 0; i < divs.length; i++)
divs[i].style.display = "none";
if(divs[i].id === id)
divs[i].style.display = "block";








To execute this when the app is loaded, the following is added at the beginning of the script:



var identity = "Settings-Settings.js";
Hook.bind(identity + "-create", init);


The -create hook then gets called when everything else has loaded so no errors occur.



When you logout and log back in, it will throw errors due to the variables already existing. I solved that by doing this:



Hook.bind(identity + "-destroy", destroy);
function destroy()
identity = null;
delete identity;
elems = null;
delete elems;
divs = null;
delete divs;




The -destroy hook gets called when the apps are removed on logout.



Surprisingly, this all works fairly well. Unfortunately, theres still the issue of scripts from different classes overwriting each others variables. If I create another app with a similar structure:



var identity = "Customers-Customers.js";

Hook.bind(identity + "-create", init);
Hook.bind(identity + "-destroy", destroy);

var elems;
var divs;

function init()
elems = document.getElementsByClassName("customer-item");
divs = document.getElementsByClassName("customer-item-content");
elems[0].classList.add("is-active");
divs[0].style.display = "block";

for(let i = 0; i < elems.length; i++)
elems[i].addEventListener("click", onItemClick);




function destroy()
identity = null;
delete identity;
elems = null;
delete elems;
divs = null;
delete divs;




function onItemClick(event)
for(let i = 0; i < elems.length; i++)
elems[i].classList.remove("is-active");
alert(elems[i].id);


event.target.classList.add("is-active");

let id = event.target.id + "-content";

for(let i = 0; i < divs.length; i++)
divs[i].style.display = "none";
if(divs[i].id === id)
divs[i].style.display = "block";








Both scripts do load. BUT since they both have the same variables defined, none of them will work properly. When you look at the part that removes is-active for the customer tabs, it will loop over the items for the settings tabs instead.



I could just rename the variables but this would be a major inconvenience for the end user if they wish to install their own custom built apps. If they choose a variable name that's already in use, it will break all scripts that use it.



This is all fairly dodgy stuff, I'm wondering if theres a better way to do this so none of the scripts interfere with each other. This way the end user doesn't have to worry about the names of the variables and functions.



Any ideas?










share|improve this question


























    0















    I'm building a web control panel. The short explanation of how it works: An application is installed onto the server which then delivers all app content to the frontend.



    Each application uses Smarty for layout but should also be able to define styles and scripts for dynamic content (start content is loaded in by php).



    The backend in php simply provides an array of these apps with their name, author, version, html content, icons, css files and js files. This is all stored in js classes and the scripts are loaded at the end with jquery's getscript function.



    The problem with these scripts is that it's kind of a mess to get them all working side by side.



    Let's say I have a settings app with multiple tabs, the javascript could contain something like this for the tab switching:



    var elems;
    var divs;
    function init()
    elems = document.getElementsByClassName("settings-item");
    divs = document.getElementsByClassName("settings-item-content");
    elems[0].classList.add("is-active");
    divs[0].style.display = "block";

    for(let i = 0; i < elems.length; i++)
    elems[i].addEventListener("click", onItemClick);




    function onItemClick(event)
    for(let i = 0; i < elems.length; i++)
    elems[i].classList.remove("is-active");


    event.target.classList.add("is-active");

    let id = event.target.id + "-content";

    for(let i = 0; i < divs.length; i++)
    divs[i].style.display = "none";
    if(divs[i].id === id)
    divs[i].style.display = "block";








    To execute this when the app is loaded, the following is added at the beginning of the script:



    var identity = "Settings-Settings.js";
    Hook.bind(identity + "-create", init);


    The -create hook then gets called when everything else has loaded so no errors occur.



    When you logout and log back in, it will throw errors due to the variables already existing. I solved that by doing this:



    Hook.bind(identity + "-destroy", destroy);
    function destroy()
    identity = null;
    delete identity;
    elems = null;
    delete elems;
    divs = null;
    delete divs;




    The -destroy hook gets called when the apps are removed on logout.



    Surprisingly, this all works fairly well. Unfortunately, theres still the issue of scripts from different classes overwriting each others variables. If I create another app with a similar structure:



    var identity = "Customers-Customers.js";

    Hook.bind(identity + "-create", init);
    Hook.bind(identity + "-destroy", destroy);

    var elems;
    var divs;

    function init()
    elems = document.getElementsByClassName("customer-item");
    divs = document.getElementsByClassName("customer-item-content");
    elems[0].classList.add("is-active");
    divs[0].style.display = "block";

    for(let i = 0; i < elems.length; i++)
    elems[i].addEventListener("click", onItemClick);




    function destroy()
    identity = null;
    delete identity;
    elems = null;
    delete elems;
    divs = null;
    delete divs;




    function onItemClick(event)
    for(let i = 0; i < elems.length; i++)
    elems[i].classList.remove("is-active");
    alert(elems[i].id);


    event.target.classList.add("is-active");

    let id = event.target.id + "-content";

    for(let i = 0; i < divs.length; i++)
    divs[i].style.display = "none";
    if(divs[i].id === id)
    divs[i].style.display = "block";








    Both scripts do load. BUT since they both have the same variables defined, none of them will work properly. When you look at the part that removes is-active for the customer tabs, it will loop over the items for the settings tabs instead.



    I could just rename the variables but this would be a major inconvenience for the end user if they wish to install their own custom built apps. If they choose a variable name that's already in use, it will break all scripts that use it.



    This is all fairly dodgy stuff, I'm wondering if theres a better way to do this so none of the scripts interfere with each other. This way the end user doesn't have to worry about the names of the variables and functions.



    Any ideas?










    share|improve this question
























      0












      0








      0








      I'm building a web control panel. The short explanation of how it works: An application is installed onto the server which then delivers all app content to the frontend.



      Each application uses Smarty for layout but should also be able to define styles and scripts for dynamic content (start content is loaded in by php).



      The backend in php simply provides an array of these apps with their name, author, version, html content, icons, css files and js files. This is all stored in js classes and the scripts are loaded at the end with jquery's getscript function.



      The problem with these scripts is that it's kind of a mess to get them all working side by side.



      Let's say I have a settings app with multiple tabs, the javascript could contain something like this for the tab switching:



      var elems;
      var divs;
      function init()
      elems = document.getElementsByClassName("settings-item");
      divs = document.getElementsByClassName("settings-item-content");
      elems[0].classList.add("is-active");
      divs[0].style.display = "block";

      for(let i = 0; i < elems.length; i++)
      elems[i].addEventListener("click", onItemClick);




      function onItemClick(event)
      for(let i = 0; i < elems.length; i++)
      elems[i].classList.remove("is-active");


      event.target.classList.add("is-active");

      let id = event.target.id + "-content";

      for(let i = 0; i < divs.length; i++)
      divs[i].style.display = "none";
      if(divs[i].id === id)
      divs[i].style.display = "block";








      To execute this when the app is loaded, the following is added at the beginning of the script:



      var identity = "Settings-Settings.js";
      Hook.bind(identity + "-create", init);


      The -create hook then gets called when everything else has loaded so no errors occur.



      When you logout and log back in, it will throw errors due to the variables already existing. I solved that by doing this:



      Hook.bind(identity + "-destroy", destroy);
      function destroy()
      identity = null;
      delete identity;
      elems = null;
      delete elems;
      divs = null;
      delete divs;




      The -destroy hook gets called when the apps are removed on logout.



      Surprisingly, this all works fairly well. Unfortunately, theres still the issue of scripts from different classes overwriting each others variables. If I create another app with a similar structure:



      var identity = "Customers-Customers.js";

      Hook.bind(identity + "-create", init);
      Hook.bind(identity + "-destroy", destroy);

      var elems;
      var divs;

      function init()
      elems = document.getElementsByClassName("customer-item");
      divs = document.getElementsByClassName("customer-item-content");
      elems[0].classList.add("is-active");
      divs[0].style.display = "block";

      for(let i = 0; i < elems.length; i++)
      elems[i].addEventListener("click", onItemClick);




      function destroy()
      identity = null;
      delete identity;
      elems = null;
      delete elems;
      divs = null;
      delete divs;




      function onItemClick(event)
      for(let i = 0; i < elems.length; i++)
      elems[i].classList.remove("is-active");
      alert(elems[i].id);


      event.target.classList.add("is-active");

      let id = event.target.id + "-content";

      for(let i = 0; i < divs.length; i++)
      divs[i].style.display = "none";
      if(divs[i].id === id)
      divs[i].style.display = "block";








      Both scripts do load. BUT since they both have the same variables defined, none of them will work properly. When you look at the part that removes is-active for the customer tabs, it will loop over the items for the settings tabs instead.



      I could just rename the variables but this would be a major inconvenience for the end user if they wish to install their own custom built apps. If they choose a variable name that's already in use, it will break all scripts that use it.



      This is all fairly dodgy stuff, I'm wondering if theres a better way to do this so none of the scripts interfere with each other. This way the end user doesn't have to worry about the names of the variables and functions.



      Any ideas?










      share|improve this question














      I'm building a web control panel. The short explanation of how it works: An application is installed onto the server which then delivers all app content to the frontend.



      Each application uses Smarty for layout but should also be able to define styles and scripts for dynamic content (start content is loaded in by php).



      The backend in php simply provides an array of these apps with their name, author, version, html content, icons, css files and js files. This is all stored in js classes and the scripts are loaded at the end with jquery's getscript function.



      The problem with these scripts is that it's kind of a mess to get them all working side by side.



      Let's say I have a settings app with multiple tabs, the javascript could contain something like this for the tab switching:



      var elems;
      var divs;
      function init()
      elems = document.getElementsByClassName("settings-item");
      divs = document.getElementsByClassName("settings-item-content");
      elems[0].classList.add("is-active");
      divs[0].style.display = "block";

      for(let i = 0; i < elems.length; i++)
      elems[i].addEventListener("click", onItemClick);




      function onItemClick(event)
      for(let i = 0; i < elems.length; i++)
      elems[i].classList.remove("is-active");


      event.target.classList.add("is-active");

      let id = event.target.id + "-content";

      for(let i = 0; i < divs.length; i++)
      divs[i].style.display = "none";
      if(divs[i].id === id)
      divs[i].style.display = "block";








      To execute this when the app is loaded, the following is added at the beginning of the script:



      var identity = "Settings-Settings.js";
      Hook.bind(identity + "-create", init);


      The -create hook then gets called when everything else has loaded so no errors occur.



      When you logout and log back in, it will throw errors due to the variables already existing. I solved that by doing this:



      Hook.bind(identity + "-destroy", destroy);
      function destroy()
      identity = null;
      delete identity;
      elems = null;
      delete elems;
      divs = null;
      delete divs;




      The -destroy hook gets called when the apps are removed on logout.



      Surprisingly, this all works fairly well. Unfortunately, theres still the issue of scripts from different classes overwriting each others variables. If I create another app with a similar structure:



      var identity = "Customers-Customers.js";

      Hook.bind(identity + "-create", init);
      Hook.bind(identity + "-destroy", destroy);

      var elems;
      var divs;

      function init()
      elems = document.getElementsByClassName("customer-item");
      divs = document.getElementsByClassName("customer-item-content");
      elems[0].classList.add("is-active");
      divs[0].style.display = "block";

      for(let i = 0; i < elems.length; i++)
      elems[i].addEventListener("click", onItemClick);




      function destroy()
      identity = null;
      delete identity;
      elems = null;
      delete elems;
      divs = null;
      delete divs;




      function onItemClick(event)
      for(let i = 0; i < elems.length; i++)
      elems[i].classList.remove("is-active");
      alert(elems[i].id);


      event.target.classList.add("is-active");

      let id = event.target.id + "-content";

      for(let i = 0; i < divs.length; i++)
      divs[i].style.display = "none";
      if(divs[i].id === id)
      divs[i].style.display = "block";








      Both scripts do load. BUT since they both have the same variables defined, none of them will work properly. When you look at the part that removes is-active for the customer tabs, it will loop over the items for the settings tabs instead.



      I could just rename the variables but this would be a major inconvenience for the end user if they wish to install their own custom built apps. If they choose a variable name that's already in use, it will break all scripts that use it.



      This is all fairly dodgy stuff, I'm wondering if theres a better way to do this so none of the scripts interfere with each other. This way the end user doesn't have to worry about the names of the variables and functions.



      Any ideas?







      javascript jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 22:02









      user265889user265889

      327417




      327417






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270750%2flooking-for-a-better-way-to-dynamically-load-javascript-for-app-style-content%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270750%2flooking-for-a-better-way-to-dynamically-load-javascript-for-app-style-content%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế