Integrating Custom JavaScript Code

Hello 

I had the following Javascript code, that I used to load using this method inside the init class...

elgg_register_js('js.curriculum', '/mod/curriculum/js/curriculum.js?v=1.0.0');
elgg_load_js('js.curriculum');
 
And the javascript file looked like this..

function syncDropBox(email,guid) {

        alert("Upgrade in progress!")

        //var URLToOpen  = "http://localhost/MOBCodeIgniter/index.php/mobdropbox/request_dropbox?e=" + email + "&g=" + guid;

        //var title = 'Sync_DropBox';

        //loadPopup(URLToOpen, title, 400, 400, "yes");

 

}

 

function uploadBook(contextPath,guid) {

        var URLToOpen  = contextPath + "/action/BookUpload.php?g=" + encodeURIComponent(guid);

        var title = 'Upload Book';

        loadPopup(URLToOpen, title, 550, 635, "no");

 

}

Now if I am trying to load the Same Javascript file in 5.1, do I need to change the javascript format and so registered my app

as a Bootstrap.php and then tried to load the javascript file using the method...

elgg_require_js('js.curriculum', '/mod/curriculum/js/curriculum.js?v=1.0.0');

But this does not work..

 

  • Just add this code to init() function your Bootstrap class:

    elgg_register_external_file('js', 'functions', elgg_get_simplecache_url('js/functions.js'));
    
    elgg_load_external_file('js', 'functions');
     
    Where functions.js is located on /mod/my_plugin/views/default/js/ folder.
     
    That's all.
     
    You can use this code
     
    elgg_load_external_file('js', 'functions');
    only on the special views/pages instead of site globaly.
     
  • I am still getting the error....Can you please go through my code and comment if I had done it right...

    This flow was working with older version, but I can't figure out the error.....

    So giving you a little trouble, for I tried to change it to the latest version

    <?php

     

    namespace Elgg\Curriculum;

     

    use Elgg\DefaultPluginBootstrap;

    use Elgg\I18n\NullTranslator;

     

    class Bootstrap extends DefaultPluginBootstrap {

     

            /**

             * {@inheritDoc}

             */

            public function boot() {

                    $this->curriculum_init();

            }

    function curriculum_init() {

     elgg_register_menu_item('site', array(

                    'name' => 'curriculum',

                    'text' => elgg_echo('curriculum'),

                    'href' => 'curriculum',

                    'href' => 'curriculum/available'

            ));

     

           elgg_register_external_file('js', 'curriculum', elgg_get_simplecache_url('js/curriculum.js'));

     

           elgg_load_external_file('js', 'curriculum');

           $this->curriculum_page_handler('curriculum');

            $this->curriculum_page_menu('register', 'menu:page')

    }

    function curriculum_page_handler($page) {

           elgg_push_breadcrumb(elgg_echo('curriculum'), 'curriculum/available');

           

    if (!isset($page[0])) {

                    $page[0] = 'available';

            }

            

            $vars = array();

            $vars['page'] = $page[0];

            

            $pages = dirname(__FILE__) . './pages/curriculum';

            switch ($page [0]) {

                    case "available" :

                            include "$pages/available.php";

                            break;

                    case "teach" :

                            include "$pages/teach.php";

                            break;

                    

                    case "learn" :

                            include "$pages/learn.php";

                            break;

                    default :

                            return false;

            }

            

            elgg_pop_context();

            

            return true;

            

          }

     

     

    function curriculum_page_menu($hook, $type, $return, $params) {

             if (elgg_is_logged_in()) {

                    // only show side links in curriculum pages

                    if (elgg_in_context('curriculum')) {

                            $page_owner = elgg_get_page_owner_entity();

                            if (!$page_owner) {

                                    //$page_owner = elgg_get_logged_in_user_entity();

                            }else{

                                    $return[0] = new ElggMenuItem('dropbox', elgg_echo('curriculum:dropbox'), 'javascript:syncDropBox("'.$enc>

                                    //$return[1] = new ElggMenuItem('uploadBook', elgg_echo('curriculum:uploadbook') , 'javascript:uploadBook>

                            }

                    }

            }

            

            return $return;

            }

     

  • This is not relevant to the question you asked.

    All I can answer you here is that you cannot include non-existent functions that are not in the extended class.

    For example, this code:

    function curriculum_init() {}

    Use

    function init() {} 

    instead.

    Look at My plugin skeleton plugin

    I think you need to learn more about PHP and Elgg or hire someone.

  • The Curriculum_int() function is working for I am able to register the menu...The java script in not loading

    I used to program in Elgg 7-8 years back and am mostly a java developer....

    I have not followed what has changed in Php and elgg and am confused a little, and so i am trying to understand it by little trial and error while trying to tweak my old app to the new design... This is how I can register what all has changed by asking questions...And some changes might not register

    To hire a developer, I need to know what to ask for and so don't mind my questions....for I am still trying to figure out whether the design that worked in the past is relevant.. So don't mind my childish questions

    I figured out a few things about Bootstrap Function or loading external javascript, but don't see the error logs to understand where the mistake is...

    HelloWorld Plugin is basic and I understand this...

    Do you have a tutorial to load an external javascript file in Bootstrap function...

    And how much would a developer charge to convert my old app to the new one...