-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unregistered functions #32
Comments
Thanks! |
I'm having a problem with this one though--because those two functions haven't been registered as dependencies, and another test is "shouldn't resolve unregistered dependencies", my inject function ignores them and only parses 1 argument as a result. is there a way to work around this? |
@emmalovato The key here is that you want to ignore the functions being passed in when you call inject that are not actually functions by not trying to replace them with registered functions. However, the function that you return from inject should not be affected by this. If that function returns the arguments that were originally passed in, you don't want to override that. You just want to make sure that any registered functions that were passed in can actually be called from inside the function. Make sense? |
maybe--are you saying we should keep the array the right length but just add an undefined element if inject is called with an unregistered parameter? I'm not really sure what you mean by "If that function returns the arguments that were originally passed in, you don't want to override that" |
the test failed for me because it is doing what (I think) you want to do, to ignore the unregistered functions, so it actually returns 1 because only Auth is defined in the parameters. but the test file expects inject() to equal to 3. is that a typo or am I missing something? |
@emmalovato @jeanmariano Sorry, I should have been clearer about this at the beginning. This is admittedly confusing. The way to handle this is, as @emmalovato suggests, to add an undefined element if inject is called with an unregistered parameter. If you add an inject function that used Array's map() method for example, you might have something like this for your inject function: function inject (func) {
var params = parseFunctionParams(func);
return function () {
var dependenciesToPassIn = params.map(function(param) {
// logic to find out if this param is a registered function
// either in this module or in one of the modules that this
// module depends on
// if so, return the registered function, something like:
// if (funcIsRegistered(param)) {
// return getRegisteredFunc(param);
// }
});
// we now have an array of dependenciesToPassIn, in order, where
// any unregistered dependencies will be passed in as undefined
return func.apply(null, dependenciesToPassIn);
};
} The above is just a suggestion for the inject function. You probably want to first try to look up the registered function on the current module and return it if you find it, and then, if that fails try to look it up on modules that the current module requires. Either way, if you map the parameters to registered functions, the default for parameters where you don't find a function will be undefined, so you'll end up with the arguments that are passed including undefined in each place where the dependency to inject doesn't exist, something like: [ undefined, undefined, [Function: Auth] ] I hope this helps. If you're still confused please reply to this issue again. |
Great, this definitely clears the issue up for me at least. The only thing I was doing differently was neglecting to add the undefined parameter, so my array of dependencies to pass in in that example just looked like |
@emmalovato Awesome, glad to hear it! |
@zgleicher asked: For the test, "shouldn't treat the parameters in nested functions as dependencies" in DI.spec.js simple, parameters 'app' and 'login' have not been registered. 'login' does not even exist. Should it be Auth and User instead?
My response:
@zgleicher Yes, it probably should be Auth and User but your tests should pass regardless. I believe at some point I renamed app and and long to Auth and User, but ended up neglecting this test because it passes anyway, since this particular test is testing how many arguments your inject method has parsed and making sure it doesn't parse nested functions, rather than actually making use of any of those nested injected functions as it does in other tests. Good catch!
The text was updated successfully, but these errors were encountered: