0

I have a variable currently set globally, that I am using in two different require() functions. How can I have it passed from first require() to the other?

Rahul Desai
  • 15,242
  • 19
  • 83
  • 138

1 Answers1

2

As far as I know: you don't. The only way to make them read the same data is by putting it somewhere so they both can read it. Because both require() functions are placed inside a normal JavaScript file, the only scope they share is the global scope.

The other possibility is by passing it to the callback function of the require(), but that means you have to write a "module" that returns your data (or use dojo/text plugin to read a file read-only).

It's not recommended to have two require() statements at all. If we're still talking about the same stopwatch (I've read your code a bit), you should put all event handlers in one require() block. Then you can put the data they both need inside that require() block and they will all be able to read it.

Some reasons why having multiple require() blocks is not beneficial:

  • Harder to read/maintain: If you have multiple require() blocks, it's confusing. Most people don't use it anyways (recommended or not).
  • More duplicates: If you're using common modules like dojo/query and dojo/dom, you will have to add them to your module list every time. I don't think it will download the file twice, but it's still more to write/maintain.
  • Scope issues: As you just noticed, you will have scoping issues like this when multiple require() blocks needs to access the same data. This means you have to put them in global scope (which is another bad practice), write a module with just plain data (not really useful) or you have to wrap both of them in a function, which you have to execute manually (confusing)
  • Seperation concerns: If you have a valid reason to seperate them, you should be seperating them into modules.
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • Why is it not recommended to have two `require()` statements? – Rahul Desai Jan 10 '14 at 07:12
  • 1
    For reasons like this, because it's confusing to read, lots of duplicate things you need to write (like common module imports such as `dojo/query`), harder to maintain and above all there must be something wrong in your design if you want to do that. If you seperate `require()` blocks, I suppose you have a reason to seperate them, if you have a valid reason you should be defining seperate modules in stead. – g00glen00b Jan 10 '14 at 07:16
  • Looks like your stopwatch is improved a lot. I was already thinking it might never work by adding an interval of 1ms, your browser wouldn't be able to handle that ;) – g00glen00b Jan 10 '14 at 07:22
  • I have now set it to 25ms as per Niet's suggestion here http://stackoverflow.com/questions/19973048/milliseconds-out-of-sync/19973169?noredirect=1#comment31598257_19973169 – Rahul Desai Jan 10 '14 at 09:18
  • Can you help me with this question? http://stackoverflow.com/questions/22678477/how-to-search-in-2-columns-with-single-search-box – Rahul Desai Mar 27 '14 at 12:03