solved Loading External JS via Modules

SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
GOAL : Load external custom JavaScript libraries: Examples maybe like JQueryUI, or JQWidgets, etc...
IDEAS:
- Could we load these via script in template: meta.tpl ? This method seems like if you change skins, you may lose your custom JS load code?
- Prefer to write new custom JR module like "sdJSLoader", and load all our external JS libs in one custom JR module. Using JR module seems easier to include and manage across our many JR sites.
- Need the best way to load external JS libs in one place, so we can make them available to all other custom JR modules.
--
Simple Example - how to achieve this type of loading external JS libs:
<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript" src="http://cdn.com/jqwidgets/jqxcore.js"></script>
</head>
--
Question: How to load external JS - like code above - except instead using a custom JR module to do the loading?
updated by @softdesigns: 05/30/17 10:32:03PM
brian
@brian
7 years ago
10,148 posts
Just add whatever JS you need to your custom skin's meta.tpl file - that's the easiest way.

Hope this helps!


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
nate
@nate
7 years ago
917 posts
Building a module to replace 1 line of code in the meta.tpl, seems like extra work for no real gain.
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Sure, we can add code to meta.tpl :)
I guess we must remember if we switch skins, we must add code again to new skin?
--
Question: If we add the code to meta.tpl - then later on, switch skins, do we lose the custom code in meta.tpl?
updated by @softdesigns: 02/19/17 11:23:13AM
nate
@nate
7 years ago
917 posts
How often do you switch skins? Just copy the line to the new skin.
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Actually in production, it could be many lines of code depending on the libraries, and required css files. Also we currently have 9 JR sites, and we add new JR sites often, so the code may need to be replicated and updated across many sites. Most JS libraries require ongoing updates, so loose script can start to be painful to maintain correct JS lib versions across many JR sites.
--
This is where a single sdJSLoader module might be easier for maintenance, and multi site JS lib version updates over the long term.
updated by @softdesigns: 02/19/17 11:22:27AM
nate
@nate
7 years ago
917 posts
The number of lines really doesn't matter. It's still copy and paste. The way jamroom works, each module loads whatever js it needs. It seems to work really well.

If you feel like you need a module to do this, feel free to create one. A module would still need to be installed and configured, which would take longer than just pasting a few lines.
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Yes - you are right - we should consider the time to install and configure a module, versus copy / paste code into meta.tpl
nate:
The way jamroom works, each module loads whatever js it needs. It seems to work really well.
I think what you said above may help us out - just let each module load it's own JS.
--
Question: Can you send a code sample of how a module loads the JS it needs?
updated by @softdesigns: 02/20/17 01:46:10AM
nate
@nate
7 years ago
917 posts
You'll wanna look at the include.php of an existing module.

This code comes from the jrAction module. The file is jrAction.js. You would only change params 3 and 4.
jrCore_register_module_feature('jrCore', 'javascript', 'jrAction', 'jrAction.js');

You need to create a js directory in your module.
updated by @nate: 02/19/17 11:45:11AM
michael
@michael
7 years ago
7,714 posts
A sample include.php file for a module called xxExternajs
<?php
/**
 * @copyright 2017 Talldude Networks, LLC.
 */

// make sure we are not being called directly
defined('APP_DIR') or exit();

/**
 * meta
 */
function xxExternaljs_meta(){
    $_tmp = array(
        'name'        => 'External Js',
        'url'         => 'externaljs',
        'version'     => '1.0.0',
        'developer'   => 'The Jamroom Network, &copy;' . strftime('%Y'),
        'description' => 'include some javascript files',
        'category'    => 'custom',
        'license'     => 'mpl'
    );
    return $_tmp;
}

/**
 * init
 */
function xxExternaljs_init(){
    jrCore_register_module_feature('jrCore', 'javascript', 'xxExternaljs', 'somefile.js');  // includes /modules/xxExternaljs/js/somefile.js
    jrCore_register_module_feature('jrCore', 'javascript', 'xxExternaljs', '//cdn.com/jqwidgets/jqxcore.js'); // includes http?://cdn.com/jqwidgets/jqxcore.js
    return true;
}
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Great Support - thanks for the code samples and info...
--
More forum code samples, help create more JR pro level coders...
Great to promote more samples, and deeper study of JR platform coding :)
--
Resolved...
updated by @softdesigns: 02/20/17 01:50:15AM

Tags