How can I use lodash inside my plugin client side script?



  • Hi,

    I'm new with nodeBB and I'm writing my first plugin.
    I have a question that may be simple but I just can't figure out how to use lodash or underscore inside my client side JS file.

    I have the file loaded, since I put this into plugin.json:

    "scripts": [
        "client/index.js"
      ],
    

    So in my index.js file:

    ...
    define('forum/client/plugins/my-plugin', [], function () {
        'use strict';
        require(['require', 'lodash'], function (require) {
            var _ = require('lodash');
    
            var Edit = {};
    
            Edit.init = function () {
                // an exemple code from lodash usage
                _.map([4, 8], function(n) {
                    return n * n;
                });
            };
    ...
    

    Can someone please point me to how to use lodash on my client side scripts?

    Thanks


  • Community Rep

    @marcelo-lopes

    That's close. You would actually just put it in your define call.

    define('forum/client/plugins/my-plugin', ['lodash'], function (_) {
        var Edit = {};
    
        Edit.init = function () {
            // an exemple code from lodash usage
            var test = _.map([4, 8], function(n) {
                return n * n;
            });
    
            console.log(test);
        };
    
        return Edit;
    });
    

    Assuming 'lodash' is a url to lodash.js



  • That's great. Thanks @yariplus!


  • Global Moderator

    @marcelo-lopes you can also define modules in plugin.json that can be required directly like define('thing', ['lodash'] instead of specifying the full url.



  • ahh nice! Thanks @PitaJ
    I could not find any info on the docs:

    will give it a try.

    Thanks


Log in to reply
 

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.