Skip to content

Propagating runtime arguments

Jasper Blues edited this page May 1, 2014 · 13 revisions

The following example shows the following concepts:

  • Propagating a runtime argument through to a dependency (setLoyaltyAccounts method)
  • Using runtime arguments with ciruclar dependencies (delegate property)
- (id)loyaltyManagementControllerWithAuthorizationKey:(NSString*)authorizationKey loyaltyAccounts:(NSArray*)loyaltyAccounts;
{
    return [TyphoonDefinition withClass:[LoyaltyManagementViewController class] configuration:^(TyphoonDefinition* definition)
    {
        [definition useInitializer:@selector(initWithAuthorizationKey:loyaltyClient:view:) parameters:^(TyphoonMethod* initializer)
        {
            [initializer injectParameterWith:authorizationKey];
            [initializer injectParameterWith:[_networkComponents loyaltyClient]];
            [initializer injectParameterWith:[self loyaltyManagementViewWithAuthorizationKey:authorizationKey
                loyaltyAccounts:loyaltyAccounts]];
        }];
        [definition injectProperty:@selector(assembly)];
        [definition injectProperty:@selector(title) with:@"Loyalty Accounts"];
    }];
}

- (id)loyaltyManagementViewWithAuthorizationKey:(NSString*)authorizationKey loyaltyAccounts:(NSArray*)loyaltyAccounts
{
    return [TyphoonDefinition withClass:[LoyaltyManagementView class] configuration:^(TyphoonDefinition* definition)
    {
        //The loyaltyAccounts parameter is propagated through the parent controller onto the view
        [definition injectMethod:@selector(setLoyaltyAccounts:) parameters:^(TyphoonMethod* method)
        {
            [method injectParameterWith:loyaltyAccounts];
        }];

        //The delegate is a circular dependency, passing the two parameters back. 
        [definition injectProperty:@selector(delegate)
            with:[self loyaltyManagementControllerWithAuthorizationKey:authorizationKey loyaltyAccounts:loyaltyAccounts]];
    }];
}