Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Layer Callbacks #1

Merged
merged 3 commits into from over 1 year ago

2 participants

Paris Holley Dave Elkan
Paris Holley

Handy feature for invoking a callback after a layer has been loaded. In my use case, i was using everyauth to setup different modules and once they all loaded, I wanted to begin my express middleware. Thought about having a special name in that layer directory, but this way seemed cleaner.

Dave Elkan dave-elkan merged commit d92d651 into from September 14, 2012
Dave Elkan dave-elkan closed this September 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
13  Readme.md
Source Rendered
@@ -62,6 +62,19 @@ i.e.
62 62
 		excludePrefix: "Abstract"
63 63
 	});
64 64
 
  65
+## Layer Callbacks
  66
+
  67
+If would like to run a callback after a layer has been processed, you may do it like so:
  68
+
  69
+    layers(app, {
  70
+		rootPath: __dirname + '/layers',
  71
+		callbacks: {
  72
+			layername: function(){
  73
+				doThis();
  74
+			}
  75
+		}
  76
+	});
  77
+
65 78
 ## License
66 79
 
67 80
 (The MIT License)
7  layers.js
@@ -16,7 +16,8 @@ module.exports = function(server, options) {
16 16
     _.defaults(options, defaults);
17 17
 
18 18
     var layers = options.layers || getLayers(options.rootPath),
19  
-        excludePrefix = options.excludePrefix;
  19
+        excludePrefix = options.excludePrefix,
  20
+        callbacks = options.callbacks || {};
20 21
 
21 22
     layers.forEach(function(layer) {
22 23
         server[layer] = loadComponents(options.rootPath + "/" + layer, layer);
@@ -81,6 +82,10 @@ module.exports = function(server, options) {
81 82
             }
82 83
         });
83 84
 
  85
+        if(callbacks[layer]){
  86
+            callbacks[layer]();
  87
+        }
  88
+
84 89
         return components;
85 90
     }
86 91
 
19  test/LayersTest.js
@@ -18,3 +18,22 @@ test("Ensure loader loads layers", function(t) {
18 18
     t.equal(server.services.testService.testServiceFunction(), "A Test value", "Test service function is present and returns correct value");
19 19
     t.end();
20 20
 });
  21
+
  22
+test("test layer callback", function(t) {
  23
+    var layers = require('../layers'),
  24
+        server = {};
  25
+
  26
+    var called = false;
  27
+
  28
+    layers(server, {
  29
+        rootPath: __dirname + "/../testLayers",
  30
+        callbacks: {
  31
+            controllers: function(){
  32
+                called = true;
  33
+            }
  34
+        }
  35
+    });
  36
+
  37
+    t.ok(called, "Callback was hit");
  38
+    t.end();
  39
+});
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.