How to initialize an Elgg JS function?

Conventional jQuery function works using my_function() but elgg.my_plugin.my_function doesn't work?

  • Here's a quick way to do modules.

    Just throw your js into a real .js file in the view system

    views/default/js/myplugin/module1.js

    In your php when you need to load this js simply call

    elgg_require_js('myplugin/module1');

    Elgg will automagically know where that file is, take care of the caching, everything.  No need to use elgg_define_js if you use the view system.

    In the js file itself:

    define(['require', 'jquery', 'elgg'], function(require, $, elgg) {


        // do something

        alert('test');

        // use jquery

        $(document).on('click', '#mylink', function(e) { alert('clicked'); });

        // require some other js

        require(['myplugin/module2'], function() {

            // do something when it's loaded

        });

        // use elgg core js

        if (elgg.is_logged_in()) {

            alert('logged in');

        }
    });

  • Looks simple enough but I still wanna use what I know until it's deprecated. 

  • is it not deprecated?  I thought the old method was deprecated...

    Either way, take the plunge, I just showed you how easy it is and once you do it you'll see it's much nicer :)

  • The old way *is* essentially deprecated since Elgg 1.9. But I'm afraid Elgg isn't in fact giving deprecation warnings about it. That needs to be fixed in one of the 2.x releases.

  • You know what, I probably need to use an AMD module to re-initialize a function.

    @Matt Yeah I'm gonna switch to AMD since it looks easy enough! What will happen to js.php though?

    @Juho are you saying elgg_extend_view('elgg.js', 'my_plugin/js'); will be deprecated?

    Does 2.0 have a css folder just like js?

  • Extending the core js is still an ok solution if you need your js on *every* page.  That's rarely the case though, most of the time you need just a small amount of js in a very specific place, and that's what modules are for.  It keeps things lighter and more manageable.

    We'll have to discuss what and how to encourage people to adopt AMD.

    Re: css - it doesn't have auto-registered css at the moment as far as I know, I do far less with css than I do with js

  • Seems like AMD doesn't play well with functions. It fired up all 3 functions when it supposed to be fired by a trigger.

  • Yeah I just tried converting to AMD but it messed up all my jQuery functions. I'm going back to elgg_load_js() and use the old way.

    My JS file will be loaded once since it's gonna be ajax driven.

  • Seems like AMD doesn't play well with functions. It fired up all 3 functions when it supposed to be fired by a trigger.

    AMD is just a specification for how to build js modules. It's not something that triggers functions on its own. There must be something wrong with your code itself.

  • @juho here's a sample of my code from memory, I'm not on the computer right now. 

    $('a#link').on('click', custom_function);

    function custom_function() {

    // do elgg.get() Ajax call 

    }

    It fires every time the page loads. I even checked firebug and it shows Ajax calls.