Naci
Naci

Reputation: 231

Dojo accessing a javascript object in external file

I am trying to access a simple javascript variable / object I defined in an external file. My question is how to load it by dojo. Here is my plunker code that didn't work:

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Tutorial: Hello Dojo!</title>
</head>
<body>
    <!-- load Dojo -->
    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"
            data-dojo-config="async: true"></script>
    <script type="text/javascript">
        require({
        packages: [
        {
                name: 'myApp',
                location: window.location.href.replace(/\/$/, "")
        }]}, 
        ["dojo/dom", "myApp/config", "dojo/domReady!"], function(dom, config) {
          console.log(config.keys);
        });
    </script>
</body>
</html>

config.js

var keys = {
    "key_1": {
        "your_name": "jimmy",
        "your_msg": "hello world"
    },
    "key_2": {
        "your_name": "billy",
        "your_msg": "foo equals bar"
    }
};

As you can see I tried to load the config.js file as config, and tried to access it as config in my code. I am getting undefined in the console.

Upvotes: 0

Views: 729

Answers (2)

GibboK
GibboK

Reputation: 73908

You should define a module for your configuration file. This can be achieved using the define passing as argument your object.

Basic example:

define({ yourProperty: yourValue});

More information can be found here: https://dojotoolkit.org/documentation/tutorials/1.10/modules/

Upvotes: 0

Naci
Naci

Reputation: 231

I did some research and what I need to use looks like the dojo's define function so that config.js looks like this:

define({
  keys : {
    "key_1": {
        "your_name": "jimmy",
        "your_msg": "hello world"
    },
    "key_2": {
        "your_name": "billy",
        "your_msg": "foo equals bar"
    }
}
})

Upvotes: 1

Related Questions