Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Underscore in SuiteScript 2

I am writing a script in SS2 and I ran into doc that stated that I should do the below. I have done this and the paths are right but it doesn't seem to see underscore as it comes back as undefined, when trying to use it. Any help with this would be great, thanks define( [ 'N/email' , 'N/runtime' , 'N/search' , '/SuiteScripts/af_scripts/[email protected]/underscore' ], function( email, runtime, search, _) { function onRequest( context ) {

        var request = context.request;

        var us = _;
    }

    return {
        onRequest: onRequest
    };
} );

like image 962
jk121960 Avatar asked Sep 25 '26 08:09

jk121960


2 Answers

In my case, I used lodash to add HTML file contents to my suitelet When you define your modules you can insert the lodash library at the end just like

define(['N/file', 'N/record', 'N/search', 'N/ui/serverWidget','./lodash.js'],

but at function you should not insert anything just like

function(file, record, search, serverWidget) {

Here is a sample of code to load a file and get his contents using lodash

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
define(['N/file', 'N/record', 'N/search', 'N/ui/serverWidget','./lodash.js'],
/**
 * @param {file} file
 * @param {record} record
 * @param {search} search
 * @param {serverWidget} serverWidget
 */
function(file, record, search, serverWidget) {

    /**
     * Definition of the Suitelet script trigger point.
     *
     * @param {Object} context
     * @param {ServerRequest} context.request - Encapsulation of the incoming request
     * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
     * @Since 2015.2
     */
    function onRequest(context) {

        var templateFile = file.load({
            id: 'filePath'
        });
        //for example.
        var compiled = _.template(templateFile.getContents());

    }

    return {
        onRequest: onRequest
    };

});

Note:

I inserted the file to the same location as my suitelet in the file cabinet that why I used this relative path (./lodash.js), use the full path if the suitelet is not at the same file.

like image 151
Mohammad Mattour Avatar answered Sep 28 '26 06:09

Mohammad Mattour


I have not see how to do it that way. But the way I found that works is like this:

/**
* @NApiVersion 2.x
* @NScriptType usereventscript
*/
require.config({
  paths:{
    "coolthing":"/SuiteScripts/myFavoriteJsLibrary"
  }
});
define(['coolthing'],function (coolthing){
  return {
    beforeLoad:function beforeLoad(ctx){
      coolthing.times(2,function(){
        log.debug('log','log');
      });
    }
  };
});
like image 21
w3bguy Avatar answered Sep 28 '26 05:09

w3bguy