Customization · Dynamics CRM · Javascript · Node · RequireJS · UI

Using Node and RequireJS with CRM

It is a good practice to separate your script into separate files to improve re-usability, maintainability, readability, and other-abilities. Traditionally, you would add the scripts into CRM form in order that they need to be loaded, CRM would then load them all before it executes anything. This has its impacts in performance and hence since CRM 2013, scripts are loaded asynchronously to improve this aspect, but that’s another blog.

Build command
Use optimize=none for debugging (script does not get optimized)
Use paths.yourpath=empty to skip optimization for the specific script

node r.js -o name=main mainConfigFile=main.js out="main.js" baseUrl=. paths.crm=empty: optimize=none

Html ref
Note the data-main attribute for require.js.

<script type="text/javascript" src="ClientGlobalContext.js.aspx"></script>
<script type="text/javascript" src="require.js" data-main="main"></script>

Main Script – main.js
This script puts everything together. It specifies the path for the scripts. Then specifies dependencies for the main function to be run.

requirejs.config({
    paths: {
        jquery: 'jquery-3.1.1', //points to jquery-3-.1.1.js (optimised)
        ko: 'knockout-3.4.1', //points to knockout-3.4.1.js (optimised)
        crm: 'crm' //points to crm.js (does not get optimised)
    }
});

require(['jquery', 'ko', 'crm'],
    function ($, ko, crm) {
        //Logic here
    });

Library Script – crm.js
Custom library for abstracting CRM API calls.

define(['sdkjquery'],
    function () {
        var addOnSave = function(callback) {
            parent.window.Xrm.Page.data.entity.addOnSave(callback);
        };
        
        //Other method definition

        return {
            AddOnSave: addOnSave,
            //Other method declarations
        };
    });

Cheers – Sy

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s