⚡️Super easy way to bind ViewModel
- typealias your ViewModel which you want to inject
typealias ViewModel = DetailViewModel
- Inject viewModel
let viewController = DetailViewController()
let viewModel = DetailViewModel()
viewController.viewModel = viewModel
- Just enjoy
bindViewModel(_:)
,bindStyles()
This will be called automatically!
Just confrom your UIView or UIViewController to ViewModelBindable!
ViewModelBindable offers two binding method bindViewModel(_:), bindStyles()
- UIViewController
class DetailViewController: UIViewController {
var disposeBag = DisposeBag()
...
}
extension DetailViewController: ViewModelBindable {
typealias ViewModel = DetailViewModel
// perform binding here
func bindViewModel(viewModel: ViewModel) {
}
// This method is optional
// Implement if you need
func bindStyles() {
}
}
- UIView
class DetailView: UIView {
var disposeBag = DisposeBag()
...
}
extension DetailView: ViewModelBindable {
typealias ViewModel = DetailViewModel
// perform binding here
func bindViewModel(viewModel: ViewModel) {
}
// This method is optional
// Implement if you need
func bindStyles() {
}
}
ViewModelBindable serves two binding methods which was used in MVVM architecture.
bindViewModel()
is used for binding between View and ViewModel.
Right after UIViewController's viewDidLoad()
Right after UIView's viewModel injection
It's recommended to bind UIView's viewModel in ViewController's bindViewModel(_:) method
func bindViewModel(viewModel: ViewModel) {
headerView.viewModel = viewModel
footerView.viewModel = viewModel
}
You can initialize some stuffs in viewDidLoad / awakeFromNib to prepare binding if you use storyboard
bindStyles()
is used for styling your views. This method is optional. So implement it if you need.
- If your app support localized text or images, then styling it in 'bindStyles()'
- If your app support theme-based design that depends on user setting
- If your app does not use Storyboard
Right before UIViewController's initial viewWillAppear(_:)
call and each time traitCollectionDidChange(_:)
call
Right before each time UIView's traitCollectionDidChange(_:)
call
- RxSwift (>= 4.0.0)
- iOS 8.0+
- Xcode 7.3+
ViewModelBindable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ViewModelBindable'
ViewModelBindable is available under the MIT license. See the LICENSE file for more info.