javascript - when to define a module and when to only require files using requireJS -


i'm struggling requirejs work properly. page running fine, think i'm doing things in oh-so wrong way.

for example, on page xzy i'm adding following javascript @ end of page (the js must stay on page now, no external js-files possible)

<script type="text/javascript" language="javascript">   //<![cdata[   (function () {     require([       'async!http://maps.google.com/maps/api/js?v=3&sensor=false',       'maps/jquery.ui.map.full.min.js',       'maps/jquery.ui.map.extensions.min'       ], function() {          // ... stuff google maps        }     );   }()); //]]> </script> 

doing makes google.map , $.().gmap method globally available, shouldn't available globally.

questions:
should convert requirejs module? why?

if so, module available on other pages or "re-define" on page 123 , dependency files have been cached?

and - have convert code inside require call module.methods, call via module_name.method_name(pass_some_parameters)?

just looking @ js:

http://maps.google.com/maps/api/js?v=3&sensor=false

you can see window.google global. there's not can without google releasing amd version.

your decision regarding should create module should firstly question of readability/maintainability of js code. modules (should be), readable, reusable chunks of code/reusable abstractions rest of code can consume. should derive testing benefits - each module should easier test in isolation.

you can end many more js files if choose modular approach, , might think leads performance issues - i.e. multiple http requests. mitigated using requirejs optimiser optimise modules single file.

if convert module, yes can require other pages, , if http caching headers set up, browser may choose use cached version, saving http request (same caching heuristics apply if you've optimised every module single file).

if re-define (i assume mean copy , paste code block), dependencies listed in call require should cached browser, , therefore instantly available (depending on web server , http caching headers).

finally, yes may have refactor code bit expose new module's api. if means exposing single object methods, that's should do. process inevitably leads better code though in experience. you've had think more module's purpose is, , leads breaking coupling between pieces of code.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -