At Box, we’ve had to deal with both of these situations, and so we made T3 in such a way that it’s easy to work with global objects.
One of the best practices represented in T3 is to favor explict over implicit, and as such, we wanted a way to make dependencies on global objects explicit. While early on we tried wrapping third-party libraries in services, we later decided that some objects should just be used as-is. Today, you can use the
getGlobal() method to retrieve a reference to a global variable instead of reaching into the global scope and pulling out an object reference.
In modules and behaviors, the
getGlobal() method is available on the context object. You need only pass in the name of a variable in the global scope to retrieve a reference:
Here, the module retrieves a reference to the global
jQuery object and stores it in a local variable.
Services can also use
getGlobal(), as it is provided on the application object that is passed into the creator function:
getGlobal() instead of reaching out into the global scope directly has several advantages:
You may also find that you need to access T3 from non-T3 objects, global or otherwise. Keep in mind that T3 restricts access to modules and behaviors as part of enforcing strict loose couping. Put simply: modules and behaviors are not meant to be used by outside objects. Services, on the other hand, were made to be accessed by any type of object. As such, you can retrieve services in the global scope by using
Box.Application.getService(), for example:
This code retrieves the
myservice service from a jQuery callback. Since the
Box.Application object is both global and exposes
getService(), you have an easy way to access any T3 service from any other code.
Important: While you can call
Box.Application.getService() from anywhere, you should be careful to call it after
Box.Application.init(), as some services may rely on the initialized state of the application to function properly. Retrieving a service before the application is started can cause unexpected errors.