Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sure that when the injected function finally runs, the context i…

…s properly set.
  • Loading branch information...
commit d87c57aa13b20aaf6cb5c95f78dce5db11a7a30e 1 parent 394eaea
@iamnoah authored
View
1  inject.coffee
@@ -172,6 +172,7 @@ steal.plugins('jquery','jquery/class').then ($) ->
# injectorFor creates requires(), which is what the user sees as the injector
injectorFor = (name) ->
requires = (dependencies...,fn)->
+ fn = useInjector injector, fn # make sure the function retains the right context
# when takes a list of the dependencies and the function to inject
# and returns a function that will resolve the dependencies and pipe them into the function
useInjector injector, (args...) -> # set the context when the injected function is called
View
1  inject.js
@@ -200,6 +200,7 @@
return requires = function() {
var dependencies, fn, _i;
dependencies = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), fn = arguments[_i++];
+ fn = useInjector(injector, fn);
return useInjector(injector, function() {
var args, d, deferreds, resolve, target;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
View
21 test/qunit/inject_test.coffee
@@ -311,6 +311,27 @@ test "setting the context", ->
)();
)();
+test "retain the context", ->
+ injector = Inject
+ name: 'foo'
+ factory: ->
+ def = $.Deferred()
+ setTimeout(->
+ def.resolve 123
+ ,25)
+ def
+
+ stop(100)
+ injector( ->
+ Inject.require('foo',(foo) ->
+ equals(foo,123)
+ Inject.require('foo',(foo2) ->
+ equals(foo2,123);
+ start()
+ )()
+ )()
+ )()
+
test "capturing the current context", ->
injector = Inject
name: 'foo'
View
25 test/qunit/inject_test.js
@@ -381,6 +381,31 @@
})();
});
+ test("retain the context", function() {
+ var injector;
+ injector = Inject({
+ name: 'foo',
+ factory: function() {
+ var def;
+ def = $.Deferred();
+ setTimeout(function() {
+ return def.resolve(123);
+ }, 25);
+ return def;
+ }
+ });
+ stop(100);
+ return injector(function() {
+ return Inject.require('foo', function(foo) {
+ equals(foo, 123);
+ return Inject.require('foo', function(foo2) {
+ equals(foo2, 123);
+ return start();
+ })();
+ })();
+ })();
+ });
+
test("capturing the current context", function() {
var injector;
injector = Inject({
Please sign in to comment.
Something went wrong with that request. Please try again.