Created
January 11, 2016 10:30
-
-
Save podhmo/1efb5e7d063f63d04039 to your computer and use it in GitHub Desktop.
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'; | |
function counter($timeout){ | |
var i = 0; | |
return (function(){ | |
i += 1; | |
return i; | |
}); | |
} | |
module.exports = { | |
counter: counter | |
}; |
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'; | |
function counter2($timeout){ | |
return (function(){ | |
this.count += 1; | |
return this.count; | |
}) | |
} | |
module.exports = { | |
counter2: counter2 | |
} |
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'; | |
function counter3($timeout){ | |
var i = 0; | |
return (function(){ | |
// do something with $timeout or other services | |
i += 1; | |
return i; | |
}); | |
} | |
module.exports = { | |
counter3: counter3 | |
}; |
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'; | |
function counter($timeout){ | |
var i = 0; | |
return (function(){ | |
i += 1; | |
return i; | |
}); | |
} | |
module.exports = { | |
counter: counter | |
}; |
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'; | |
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()); | |
}); |
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' | |
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()); | |
}); |
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'; | |
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()); | |
}); |
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'; | |
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()); | |
}); |
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
{ | |
"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" | |
} | |
} |
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'; | |
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; |
Author
podhmo
commented
Jan 11, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment