Neil Ward
Neil Ward

Reputation: 31

Loading amcharts libraries in SAPUI5 - component.js vs index.html

I'm having trouble loading the amcharts libraries in SAPUI5. There are 3 amCharts libraries I want to load, core, charts and animated. I have them in my project structure in a folder called libs... please note that i want to use the component.js file not the index.html since my app will be launched from the Fiori Launchpad.

After some great advice I am now loading them in the manifest.json file like below

        "resources": {
            "js": [
                {
                    "uri": "libs/core.js"
        },
                        {
                    "uri": "libs/charts.js"
        },
                        {
                    "uri": "libs/animated.js"
        }
        ],

            "css": [
                {
                    "uri": "css/style.css"
                }
            ]
        },

when i test the app via the index.html file... it works!

when i test using the component.js file I get the following error in the console

Failed to load resource: the server responded with a status of 404 ()
The issue is most likely caused by application znrw.amCharts. Please create a support incident and assign it to the support component of the respective application. - Failed to load UI5 component with properties: '{
    "asyncHints": {
        "waitFor": []
    },
    "name": "znrw.amCharts",
    "url": "../../../../../webapp",
    "id": "application-Test-url-component",
    "componentData": {
        "startupParameters": {}
    },
    "async": true
}'. Error likely caused by:
TypeError: Cannot read property '1' of null

error in launchpad

so my question is, do I have to add something to the component.js file to get the libraries to load? or is there something more I need to do in the manifest.json file?

currently, the component file looks like this:

    "sap/ui/core/UIComponent",
    "sap/ui/Device",
    "znrw/amCharts/model/models"
], function (UIComponent, Device, models) {
    "use strict";

    return UIComponent.extend("znrw.amCharts.Component", {

        metadata: {
            manifest: "json"
        },

        /**
         * The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
         * @public
         * @override
         */
        init: function () {
            // call the base component's init function
            UIComponent.prototype.init.apply(this, arguments);

            // enable routing
            this.getRouter().initialize();

            // set the device model
            this.setModel(models.createDeviceModel(), "device");
        }
    });
});

Upvotes: 0

Views: 1074

Answers (1)

Jorg
Jorg

Reputation: 7250

In the sap.ui section of the manifest, you'll find an entry for resources. Generally it only has a CSS file in it but you can define external javascript libraries as well. If you're loading multiple dependent ones make sure to put them in order

    "resources": {
        "js": [{
            "uri": "libs/mylib.js"
        }],
        "css": [{
            "uri": "css/style.css",
            "id": "style"
        }]
    }

If all is well, you'll just be able to use window.amChart or whatever their global object is called

Upvotes: 0

Related Questions