Permalink
Browse files

allow useCurrent to ignore the fact that there is no context

  • Loading branch information...
1 parent 128ccad commit f42785d4504cb06c85ebecd2f158cf1b53f2b941 @iamnoah committed May 3, 2012
Showing with 20 additions and 7 deletions.
  1. +3 −3 inject-core.coffee
  2. +7 −3 inject-core.js
  3. +5 −0 test/qunit/inject_test.coffee
  4. +5 −1 test/qunit/inject_test.js
View
6 inject-core.coffee
@@ -146,11 +146,11 @@ useInjector = (injector,fn) ->
finally
CONTEXT.pop()
-inject.useCurrent = (fn) ->
+inject.useCurrent = (fn,ignoreNoContext) ->
context = last(CONTEXT);
- unless context
+ unless context or ignoreNoContext
noContext()
- useInjector(context,fn)
+ if context then useInjector(context,fn) else fn
noContext = ->
throw new Error("""There is no current injector.
View
10 inject-core.js
@@ -169,11 +169,15 @@
};
};
- inject.useCurrent = function(fn) {
+ inject.useCurrent = function(fn, ignoreNoContext) {
var context;
context = last(CONTEXT);
- if (!context) noContext();
- return useInjector(context, fn);
+ if (!(context || ignoreNoContext)) noContext();
+ if (context) {
+ return useInjector(context, fn);
+ } else {
+ return fn;
+ }
};
noContext = function() {
View
5 test/qunit/inject_test.coffee
@@ -355,6 +355,7 @@ test "destroy the context", ->
test "capturing the current context", ->
+ expect 2
injector = Inject
name: 'foo'
factory: -> 123
@@ -367,6 +368,10 @@ test "capturing the current context", ->
)),200)
)()
+ Inject.useCurrent(->
+ ok(true,'Can ignore no context')
+ ,true).call this
+
test "error on no context", ->
expect(1)
try
View
6 test/qunit/inject_test.js
@@ -431,19 +431,23 @@
test("capturing the current context", function() {
var injector;
+ expect(2);
injector = Inject({
name: 'foo',
factory: function() {
return 123;
}
});
stop();
- return injector(function() {
+ injector(function() {
return setTimeout(Inject.useCurrent(Inject.require('foo', function(foo) {
equals(foo, 123);
return start();
})), 200);
})();
+ return Inject.useCurrent(function() {
+ return ok(true, 'Can ignore no context');
+ }, true).call(this);
});
test("error on no context", function() {

0 comments on commit f42785d

Please sign in to comment.