ReactiveCocoa / ReactiveViewModel

Model-View-ViewModel, using ReactiveCocoa
MIT License
1.96k stars 259 forks source link

How-to MVVM with container controllers? #7

Closed oleksii-demedetskyi closed 11 years ago

oleksii-demedetskyi commented 11 years ago

Hi. I have a question about using MVVM approach with container controllers. For example. We have container that show ListViewController or MapViewController. Simple tab-bat approach. Does this container view controller require standalone ViewModel? If yes, does this view model correct:

@property RACCommand* activateMapPresentation;
@property RACCommand* activateListPresentation;
@property BOOL mapIsActive;
@property BOOL listIsActive;
jspahrsummers commented 11 years ago

You can reuse the same view model for multiple views in your hierarchy, if it makes sense to do so.

The view model hierarchy should reflect the abstract structure of the presented data, while the view layer may become more complex to actually support the desired presentation.

xNekOIx commented 11 years ago

@jspahrsummers In case we want child view controllers to have their own view models, who is responsible for creating view model for child view controllers? Is it parent view controller, parent view model or child view controller?

jspahrsummers commented 11 years ago

@xNekOIx Generally, the parent view model would create the child view model. By doing it this way, you can observe the view model to know when to present the next VC. For example:

// In the parent view controller
RACSignal *childViewController = [RACObserve(self.viewModel, childViewModel)
    map:^(ChildViewModel *viewModel) {
        return [[ChildViewController alloc] initWithViewModel:viewModel];
    }];

[self.navigationController rac_liftSelector:@selector(pushViewController:animated:) withSignals:childViewController, [RACSignal return:@YES]];