简体   繁体   中英

Using typhoon assemblied model property in another definition

I have problem with Typhoon framework. I have assembly that construct my data model:

- (DataModel *)dataModel {
    return [TyphoonDefinition withClass:[DataModel class]];
}

now I want to assembly all my view models. In one definition I need to decide if password was already set by user. This information is stored in the data model. So my definition looks like this:

- (id)passViewModel {
    return [TyphoonDefinition withClass:[PasscodeViewModel class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initwithType:) parameters:^(TyphoonMethod *initializer) {
            NSNumber *type = [self.modelAssembly dataModel] isPasscodeSet ? @(TypeReturning) : @(TypeNew);
            [initializer injectParameterWith:type];
        }];
    }];
}

The problem is that when definitions are activated, the dataModel is TyphoonDefinition, not DataModel.

Is there any method that can allows me to get value of property of dataModel?

EDIT:

According to the answer below, my assembly looks like this now:

- (UIViewController *)passcodeViewController {
    return [TyphoonDefinition withOption:[(id)[self.modelAssembly dataModel] property:@selector(isPasscodeSet)]
                                     yes:[self passcodeViewController:@(TypeReturning)]
                                      no:[self passcodeViewController:@(TypeNew)]];
}

- (UIViewController *)passcodeViewController:(NSNumber *)entryType {
    return [TyphoonDefinition withClass:[PasscodeViewController class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(entryType) with:entryType];
        [definition injectProperty:@selector(viewModel) with:[self.viewModelsAssembly passcodeViewModel:entryType]];
    }];
}

I'm using "passcodeViewController" as typhoon key in storyboard. Unfortunately the viewModel and entryType that I try to inject are nil.

You're right, an assembly can be either active or inactive so its not designed to do what you'd like to do. Therefore, there's a special feature for your use-case called Typhoon Option Matcher .

You can return one definition or another based on the start of another object in the (activated) assembly or a runtime argument. Example:

- (id)passViewModel
{
    return [TyphoonDefinition withOption:[(id)[self.modelAssembly dataModel] 
        property:@selector(isPasscodeSet)]
        yes:[self passViewModelWithType:@(TypeReturning)] 
        no:[self passViewModelWithType:@(TypeNew)]];
}

- (id)passViewModelWithType:(NSNumber *)type
{
    return [TyphoonDefinition withClass:[PasscodeViewModel class] 
      configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initwithType:) 
          parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:type];
        }];
    }];
}

Does this suit your needs? Perhaps you can combine this with factory definitions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM