In some existing Dojo application, I see a module using a singleton form template:
define([...], function(...) { var MyClass = declare(...); if (!_instance) { var _instance = new MyClass(); } return _instance; });
But if I understand AMD correctly, the use of _instance is not required, because the function passed to the define (...) call should only be executed once ... or maybe not?
My understanding of the AMD loader is that when it gets a module by calling "require" or "define", it checks to see if the module has already been loaded. If it is not loaded yet, it will load JS, execute the passed define function, and internally store the return value. If it is already loaded, it will simply return the previously saved value.
Are my assumptions correct? If so, when writing a module, we can safely assume that this module will load and execute only once, and we do not need to perform any checks to make sure that something has already been initialized or not, which the code does more simple.
Thanks.
source share