Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasBurleson/1b859ecd7be3297efd6e to your computer and use it in GitHub Desktop.
Save ThomasBurleson/1b859ecd7be3297efd6e to your computer and use it in GitHub Desktop.
Example use of module pattern and angular.module() usage to easily group classes by context... without using AMD.
(function( angular, supplant ) {
"use strict";
angular.module( "Pathways", [ ] )
/**
* Loader for topology data models
*/
.service( "nodeLoader", function NodeLoader( $http )
{
// ...
})
/**
* Create a topology diagram instance from `raw` node JSON data...
*/
.service( "topology", function Topology()
{
// ...
})
/**
* Processor will create a promise for each route and supports
* building sophisticated chains of promises.
*
*/
.service( "processor", function NodeProcessor( $q, $timeout, $log )
{
//...
});
}(window.angular, window.supplant ));
@ThomasBurleson
Copy link
Author

An extension to this idea is to have the Module pattern return an object. The above could be easily extended as follows:

var nsPathways = (function( angular, supplant ) {
    "use strict";

    var namespace = "Pathways";

    angular.module( namespace, [ ] )
                  .service( "nodeLoader", function NodeLoader( $http ){ /* ... */ } )
                  .service( "topology",   function Topology( ){ /* ... */ } )
                  .service( "processor",  function NodeProcessor( $q, $timeout, $log ){ /* ... */ } );

    return namespace;

}(window.angular, window.supplant ));

Typically I do not assign the module pattern output with the above, since the code actually registers within Angular the module and its class references.

@ThomasBurleson
Copy link
Author

And then, of course, the above can easily be used within an NG Application setup.

Let's :

<body ng-app="my.CustomApp">

</body>

<script src="./src/Pathways.js" ></script>
<script>

    var myCustomApp = angular.module( "my.CustomApp", [ nsPathways ] );

</script>

@ajoslin
Copy link

ajoslin commented May 13, 2014

A few comments:

  1. It would be easier not to have closures on the source files (save some whitespace & boilerplate), and then let the build process add closures.
  2. There is the possibility of using CommonJS with browserify that should be discussed.
  3. If the modules will be distributed individually, there will need to be a more in-depth approach for dependencies. The CI system could definitely upload each module in a tarball to a location, but it would have to be able to figure out dependent modules, and that may require some sort of bower.json or dependencies file in each component's folder. Note that the newest version of bower can install from a zip url: bower install ns-project.com/pathways.zip

edit: thought about commonJS some more, it's not worth the trouble it brings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment