Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created January 11, 2016 10:30
Show Gist options
  • Save podhmo/1efb5e7d063f63d04039 to your computer and use it in GitHub Desktop.
Save podhmo/1efb5e7d063f63d04039 to your computer and use it in GitHub Desktop.
'use strict';
function counter($timeout){
var i = 0;
return (function(){
i += 1;
return i;
});
}
module.exports = {
counter: counter
};
'use strict';
function counter2($timeout){
return (function(){
this.count += 1;
return this.count;
})
}
module.exports = {
counter2: counter2
}
'use strict';
function counter3($timeout){
var i = 0;
return (function(){
// do something with $timeout or other services
i += 1;
return i;
});
}
module.exports = {
counter3: counter3
};
'use strict';
function counter($timeout){
var i = 0;
return (function(){
i += 1;
return i;
});
}
module.exports = {
counter: counter
};
'use strict';
require('./setup')(function(angular){
var c = require('./counter');
angular
.module("app", [])
.factory("counter", ["$timeout", c.counter])
.service("useCounter1", function(counter){return {use: counter};})
.service("useCounter2", function(counter){return {use: counter};})
;
var inj = angular.injector(["ng", "app"]);
var c1 = inj.get("useCounter1");
var c2 = inj.get("useCounter2");
console.log(c1.use());
console.log(c2.use());
console.log(c1.use());
});
'use strict'
require('./setup')(function(angular){
var c = require('./counter2');
angular
.module("app", [])
.factory("counter", ["$timeout", c.counter2])
.factory("useCounter1", function(counter){
var state = {count: 0};
return {
use: counter.bind(state)
};
})
.factory("useCounter2", function(counter){
this.count = 0;
return {
use: counter.bind(this)
};
})
;
var inj = angular.injector(["ng", "app"]);
var c1 = inj.get("useCounter1");
var c2 = inj.get("useCounter2");
console.log(c1.use());
console.log(c2.use());
console.log(c1.use());
});
'use strict';
require('./setup')(function(angular){
var c = require('./counter3');
angular
.module("app", [])
.factory("useCounter1", function($timeout){
return {
use: c.counter3($timeout)
};
})
.factory("useCounter2", function($timeout){
return {
use: c.counter3($timeout)
};
})
;
var inj = angular.injector(["ng", "app"]);
var c1 = inj.get("useCounter1");
var c2 = inj.get("useCounter2");
console.log(c1.use());
console.log(c2.use());
console.log(c1.use());
});
'use strict';
require('./setup')(function(angular){
var c = require('./counter');
function counterFactory($timeout){
return function(){
return c.counter($timeout);
};
}
angular
.module("app", [])
.factory("counter", ["$timeout", counterFactory])
.service("useCounter1", function(counter){return {use: counter()};})
.service("useCounter2", function(counter){return {use: counter()};})
;
var inj = angular.injector(["ng", "app"]);
var c1 = inj.get("useCounter1");
var c2 = inj.get("useCounter2");
console.log(c1.use());
console.log(c2.use());
console.log(c1.use());
});
{
"name": "singleton-or-factory",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"angular": "^1.4.8",
"benv": "^3.0.0"
}
}
'use strict';
var benv = require('benv');
function setup(cb){
benv.setup(function(){
global.Node = window.Node;
benv.expose({
angular: benv.require(require.resolve("angular/angular"), "angular")
});
cb(window.angular);
});
}
module.exports = setup;
@podhmo
Copy link
Author

podhmo commented Jan 11, 2016

npm install
node main.js
node main4.js

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