Loading resources

You can use different types of resources in your visualization extensions, for example:

  • Style sheets / CSS files
  • JavaScript libraries
  • Images
  • Fonts
  • Items from the content library

Qlik Sense load resources asynchronously using RequireJS.

Loading resources asynchronously using RequireJS

Qlik Sense uses RequireJS to take care of the asynchronous loading of resources.

define( [ /* dependencies */ ],
    function ( /* returned dependencies as arguments */ ) {
        ...
    } );

jQuery is pre-configured as an internal dependency of Qlik Sense and there is therefore no need to explicitly load jQuery. By defining the dependency in the first parameter of the define function, you ensure jQuery is loaded and that the return value of this library will be passed as the first parameter in the function (the second argument of define).

Loading the predefined dependency of jQuery looks like the following.

define( [ "jquery" ],
    function ( $ ) {
        'use strict';
        return {

            paint: function ( $element, layout ) {
                // We can use jQuery here
                console.log($('head'));
            }
        };
    } );

Style sheets / CSS files

There are several ways of loading style sheets into your visualization extension:

  • Loading and adding the content of a CSS file to the document header
  • Adding a link to a style sheet to the document header
  • Using the RequireJS CSS plugin
Note: Make sure to prevent conflicts between existing styles or style definitions from other visualization extensions when you are creating style sheets. See Styling your visualizations.
Tip: It is good programming practice to keep your styling in a separate css file. Separating the content from the design makes it easier to maintain your visualizations.

Qlik Sense set the CSS class qv-object-[extension name] on your visualizations and your CSS rules should be prefixed with that.

Loading and adding the content of a CSS file to the document header

You can use RequireJS and the text! prefix in the define statement to inject the content into the header of the current document.

Qlik Sense includes the RequireJS plugin for loading text resources, which can be used to load a specific CSS file. Assuming you have the following file structure:

When prefixing a path with text!, the text plugin loads a file and passes its content to a variable.

define( [ 
        'jquery',
        'text!./css/myStyle.css' 
    ], function ( $, cssContent ) {

        // cssContent now contains the content of myStyle.css

        // Let's inject the CSS declarations into the header of the current document
        $( '<style>' ).html(cssContent).appendTo( 'head' );

    } );
Tip: See below for an explanation of the code example.
  • $("<style>") creates a new object.
  • The content of the cssContent variable is then assigned to the inner content of the style object.
  • The style object, now including the CSS content, is added to the <head> section of the current document.

Adding a link to a style sheet to the document header

You can add a link to the style sheet and append it to the head of the document.

define( [ 
        "jquery"
    ], function ( $ ) {

        $('<link rel="stylesheet" type="text/css" href="/extensions/my-extension/css/myStyle.css">').appendTo("head");

    } );

Using the RequireJS CSS plugin

You can use the CSS loader plugin of RequireJS to load your style sheets.

define( [ 
        "jquery",
        "css!./css/myStyle.css" 
    ], function ( $ ) {

        // Nothing more is needed

    } );
Note: Using the RequireJS CSS plugin is supported as of Qlik Sense 2.0.

JavaScript libraries

You can load local or external JavaScript files.

Loading local JavaScript files

Use RequireJS to add the local JavaScript file as a dependency:

define([
        './lib/js/extensionUtils'
],
function ( extensionUtils ) {
    'use strict';

    return {
        paint: function ( $element, layout ) {

            extensionUtils.doSomething();

        }
    };
});

Loading external JavaScript files

If you want to load resources from a Content Delivery Network (CDN), see this example:

define([
        '//code.highcharts.com/highcharts.js'
],
function ( highCharts ) {
    'use strict';

    return {
        paint: function ( $element, layout ) {

            // do something with highCharts

        }
    };
});
Tip: Loading JavaScript files from a CDN is a good approach if you use the same library in several visualization extensions since the browser does not have to load the resource all over again. It is also a good approach if you know for certain that the users of your visualization extension have Internet access.
Note:

Qlik Sense does not support exporting or printing of visualization extensions that use external resources.

Images

When including images in your visualization extension, all you need to do is referencing the path correctly so that the image works in Qlik Sense as well as Qlik Sense Desktop.

Syntax: Root path

\extensions\<YOUR_EXTENSION_NAME>\

Example:  

paint: function ( $element, layout ) {

    $element.empty();
    $element.append('<img src="/extensions/MyExtension/lib/images/pic.png" />');

}

Did this information help you?

Thanks for letting us know. Is there anything you'd like to tell us about this topic?

Can you tell us why it did not help you and how we can improve it?