Created
October 6, 2014 22:16
-
-
Save spitimage/86fe983e9622ab2c3946 to your computer and use it in GitHub Desktop.
Shim for importing AngularJS services and factories into the node environment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Illustrates a simple AngularJS factory | |
angular.module('myApp') | |
.factory('add', function () { | |
// Here is our factory state | |
var theCurrentValue = 0; | |
return { | |
// Our fancy service invocation | |
run: function (val) { | |
theCurrentValue += val; | |
return theCurrentValue; | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This module provides a shim for importing AngularJS services and factories into a node environment. Might be useful for | |
* testing/instrumenting services using the rich capabilities of the Node ecosystem. | |
* | |
* This shim only works on AngularJS services/factories that don't have other dependencies. (Injection is not supported). | |
*/ | |
var vm = require('vm'); | |
var fs = require('fs'); | |
// This object provides a context with an AngularJS shim | |
var context = { | |
angular: { | |
// Mimics the angular.module API | |
module: function () { | |
return { | |
factory: function (name, f) { | |
// Return the object that is created inside the factory function | |
return f(); | |
}, | |
service: function (name, Constructor) { | |
// Return a new object by calling the provided constructor | |
return new Constructor(); | |
} | |
} | |
} | |
} | |
}; | |
module.exports = function (filename) { | |
// Read the JS file into memory | |
var script = fs.readFileSync(filename, {encoding: 'utf8'}); | |
// Attempt to run the JS in the provided context | |
return vm.runInNewContext(script, context); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a demo program to demonstrate the AngularJS Services shim. | |
var assert = require('assert'); | |
var shim = require('./angularServiceShim'); | |
// Import and test the multiply service | |
var multiply = shim('./multiply.js'); | |
multiply.run(7); | |
multiply.run(3); | |
var result = multiply.run(2); | |
assert.equal(42, result, 'Multiply Failed'); | |
// Import and test the add factory | |
var add = shim('./add.js'); | |
add.run(7); | |
add.run(3); | |
result = add.run(2); | |
assert.equal(12, result, 'Add Failed'); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Illustrates a simple AngularJS service | |
angular.module('myApp') | |
.service('multiply', function Multiply() { | |
// Here is our factory state | |
this.theCurrentValue = 1; | |
// Our fancy service invocation | |
this.run = function (val) { | |
this.theCurrentValue *= val; | |
return this.theCurrentValue; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment