Hi all,
There are instances where the JavaScript code for my apps becomes long and difficult to navigate. I have been exploring the option of breaking the long code out into separate JavaScript modules and loading them in through "require". However is seems like i can only return one function per module. Therefore i end up calling modules from modules.
Is this the proper way to handle this? Is there a better way to break out my code into separate files which i can then call in my main.js? For example:
I need to parse json data. Instead of having all of that code for parsing in main.js (pretend it has callbacks and more than one function), I put the json parsing code in a file called parse.js. Then from main.js I access parse.js and call its functions. like the following
parse = "./parse.js"
and then simply call
parse.parseJsonFunction(api_url)
or something along those lines.
Here is my current workflow with the AMD modules. I made the code as simple as i could. Notice how I am referencing module2 from module1. Is this how this is supposed to be done?
Index.html
module1.js
Solved! Go to Solution.
Not sure why you'd assume there's only a single function in a module. Have you ever looked inside modules you're importing? they're all filled with functions - that's the whole purpose of a module.
As Dojo isn't the modern way of writing javascript (like in the new versions of ES6+), the way to separate code blocks seems a bit "hacky" and the "define" helps to create separate modules (as you already understood). Each module contains a "main" function, which is then used as a namespace.
What's the error you're getting if you add more functions? like the following -
define(
[],
function() {
return {
add: function(a,b) {
alert("entered module2")
return a + b;
},
delete: function(a,b) {
alert("another function")
return a + b;
}
}
}
)
Not sure why you'd assume there's only a single function in a module. Have you ever looked inside modules you're importing? they're all filled with functions - that's the whole purpose of a module.
As Dojo isn't the modern way of writing javascript (like in the new versions of ES6+), the way to separate code blocks seems a bit "hacky" and the "define" helps to create separate modules (as you already understood). Each module contains a "main" function, which is then used as a namespace.
What's the error you're getting if you add more functions? like the following -
define(
[],
function() {
return {
add: function(a,b) {
alert("entered module2")
return a + b;
},
delete: function(a,b) {
alert("another function")
return a + b;
}
}
}
)
Hi @shaylavi thank you for the fast response. I am glad to hear that I was wrong in my assumption. I assumed that it was only one function per module because I was unable to figure out how to allow functions to interact with each other in a module. I am new to JavaScript so I am still trying to wrap my head around things. What i mean "allow functions to interact with each other in a module" is that i cannot call a function from another function in a module as in the example below.
main.js
Hi, this is really basic javascript knowledge - I strongly suggest you find a good javascript tutorial to teach you these things, which will make your life so much easier in the long run.
To call a function within the same module you need to use "this" - this.multiply(a, b).
this points at the current scope of work, which includes all the other functions as well.
@shaylavi Thank you for your time. I will accept your first response as the answer so this question will be marked as 'solved'