From f42785d4504cb06c85ebecd2f158cf1b53f2b941 Mon Sep 17 00:00:00 2001 From: noah Date: Thu, 3 May 2012 12:52:24 -0500 Subject: [PATCH] allow useCurrent to ignore the fact that there is no context --- inject-core.coffee | 6 +++--- inject-core.js | 10 +++++++--- test/qunit/inject_test.coffee | 5 +++++ test/qunit/inject_test.js | 6 +++++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/inject-core.coffee b/inject-core.coffee index b16c18f..dd5050d 100644 --- a/inject-core.coffee +++ b/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. diff --git a/inject-core.js b/inject-core.js index b2553f3..c096ae8 100644 --- a/inject-core.js +++ b/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() { diff --git a/test/qunit/inject_test.coffee b/test/qunit/inject_test.coffee index a2fd9f0..9f49b6c 100644 --- a/test/qunit/inject_test.coffee +++ b/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 diff --git a/test/qunit/inject_test.js b/test/qunit/inject_test.js index 8743c82..3190c6c 100644 --- a/test/qunit/inject_test.js +++ b/test/qunit/inject_test.js @@ -431,6 +431,7 @@ test("capturing the current context", function() { var injector; + expect(2); injector = Inject({ name: 'foo', factory: function() { @@ -438,12 +439,15 @@ } }); 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() {