pflous
pflous

Reputation: 571

Where to bind observables in MVVM?

I've noticed on RxSwift GitHub Login Example that observables are passed from the ViewController into the ViewModel upon its initialization :

let viewModel = GithubSignupViewModel1(
            input: (
                username: usernameOutlet.rx_text.asObservable(),
                password: passwordOutlet.rx_text.asObservable(),
                repeatedPassword: repeatedPasswordOutlet.rx_text.asObservable(),
                loginTaps: signupOutlet.rx_tap.asObservable()
            ),
            dependency: (
                API: GitHubDefaultAPI.sharedAPI,
                validationService: GitHubDefaultValidationService.sharedValidationService,
                wireframe: DefaultWireframe.sharedInstance
            )
        )

Is this the right way to be binding the login tap and text fields in reactive programming?

Before seeing this example I created a function in my ViewModel onSignUpClick() and from the view controller bound clicks to run that function. Is that bad form?

Seems like if everything is passed through the init it results in a very fat init function

Upvotes: 1

Views: 355

Answers (1)

solidcell
solidcell

Reputation: 7739

It's hard to say if your previous approach was bad form or not, from just one sentence. How did the view model function have access to the field values? How did it return a result to the view controller? To give a proper response, we'd need to see code.

As for the "fat init", it might be personal preference, but I don't see a problem with it. One way or another, those inputs are going to be dealt with somewhere. So one line in the init seems pretty slim for a view controller in the end to me.

Upvotes: 1

Related Questions