Skip to content

Support for implicit args in init methods #540

@mjclemente

Description

@mjclemente

I posted about this in Slack, and thought it worth logging here, for discussion.

I ran into an interesting issue when trying to use Eric Peterson's Hyper library with FW/1. I set the defaults, as laid out in the docs, in my Loadlistener, like this:

.declare( 'apiClient' ).instanceOf( 'hyper.models.HyperBuilder' ).asSingleton().withOverrides(
  {
    baseUrl = 'https://thehost.com/api/v1/whatever',
    headers = {
      "Token" = "xxx-xxx-xxx"
    },
    timeout = 12
  }
).done()

While the overrides get picked up, the ultimately don't get applied to the HyperBuilder cfc, because it doesn't explicit list its args or properties. It just looks for them in the args scope. The init method looks like this:

function init() {
this.defaults = new Hyper.models.HyperRequest();
for ( var key in arguments ) {
  invoke(
    this.defaults,
    "set#key#",
    { 1 : arguments[ key ] }
  );
}
return this;
}

If I modify the init method to explicitly include the args (baseUrl, headers, timeout), then it works as expected.

Basically, I expected FW/1 to pass my overrides into the init method, whether or not they were defined there explicitly, and that's not happening.

I did find that I can generate the behavior I'm looking for with a small change to ioc.cfc. If I add the following around line 879:

if( isEmpty( info.metadata.constructor ) ){
  args = overrides;
} 

Basically, if there's no constructor args, but there are overrides, use them when creating the bean.

Does this seem like a change worth making?

Finally, I should note that some really helpful suggestions were made for working around this limitation. I ultimately did something like this:

di1.declare( "apiClient" )
  .asValue( new hyper.models.HyperBuilder(   
    baseUrl = 'https://thehost.com/api/v1/whatever',
    headers = {
            "Token" = "xxx-xxx-xxx"
    },
    timeout = 12
 ) );

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions