https://github.com/rxswiftcommunity/rxbinding
Simple data binding operators ~> and <~> for RxSwift.
https://github.com/rxswiftcommunity/rxbinding
data-binding operators rswift
Last synced: 11 months ago
JSON representation
Simple data binding operators ~> and <~> for RxSwift.
- Host: GitHub
- URL: https://github.com/rxswiftcommunity/rxbinding
- Owner: RxSwiftCommunity
- License: mit
- Created: 2019-04-12T02:48:20.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-01-15T12:36:25.000Z (about 5 years ago)
- Last Synced: 2025-04-19T22:36:34.675Z (11 months ago)
- Topics: data-binding, operators, rswift
- Language: Swift
- Homepage:
- Size: 76.2 KB
- Stars: 62
- Watchers: 8
- Forks: 14
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RxBinding
[](https://travis-ci.org/RxSwiftCommunity/RxBinding)
[](https://cocoapods.org/pods/RxBinding)
[](https://cocoapods.org/pods/RxBinding)
[](https://cocoapods.org/pods/RxBinding)
[](https://github.com/RxSwiftCommunity/RxBinding/releases)
RxBinding provides `~>`, `<~>` and `~` operators for data binding using RxSwift, to replace the `bind(to:)` and `disposed(by:)` method in RxSwift.
RxBinding is inspired by the following operators.
- The `<->` operator in RxBiBinding (https://github.com/RxSwiftCommunity/RxBiBinding)
- The `<~` operator in ReactiveCocoa (https://github.com/ReactiveCocoa/ReactiveCocoa)
## Documentation
RxBinding is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'RxBinding'
```
With `@_expoerted import`, the operators can be used in the all file of the project.
```Swift
@_exported import RxBinding
```
#### Usage of `~>`
The type of `text` is `Observable` and the type of `label.rx.text` is `Binder`.
RxSwfit provides the following method for the one way data binding between them.
```Swift
viewModel.text.bind(to: label.rx.text).disposed(by: disposeBag)
```
With the operators `~>` (`bind(to:)`) and `~` (`disposed(by:)`) in RxBinding, we can bind with the following simple code.
```Swift
viewModel.text ~> label.rx.text ~ disposeBag
```
Bind an observable object to multiple binders.
```Swift
viewModel.text ~> [label1, label2].map { $0.rx.text } ~ disposeBag
```
#### Usage of `<~>`
The type of `text` is `BehaviorRelay` and the type of `textFeild.rx.text` is `ControlProperty`.
To apply the two way data binding between them, we need the following code by RxSwift.
```Swift
viewModel.text.bind(to: textFeild.rx.text).disposed(by: disposeBag)
textFeild.rx.text.bind(to: viewModel.text).disposed(by: disposeBag)
```
With the `<~>`, a simple two way bind operator, and `~` (`disposed(by:)`) in RxBinding, we can do the same thing with the following simple code.
```Swift
viewModel.text <~> textFeild.rx.text ~ disposeBag
```
#### Multiple Bindings
RxBinding supports using a single `disposeBag` for multiple binding operators like this:
```Swift
disposeBag ~ [
viewModel.text <~> textFeild.rx.text,
viewModel.uppercaseText ~> label.rx.text,
viewModel.charactersCount ~> [characterCountLabel1, characterCountLabel2].map { $0.rx.text }
]
```
or this:
```Swift
viewModel.text <~> textFeild.rx.text ~
viewModel.uppercaseText ~> label.rx.text ~
viewModel.charactersCount ~> [characterCountLabel1, characterCountLabel2].map { $0.rx.text }
~ disposeBag
```
## RxCocoa
RxBinding also supports `Driver` and `Signal` of the RxCocoa module.
You can use `~>` operator to replace the `drive()` and `emit(to:)` method.
## NEED YOUR HELP
**I am considering how to remove the operator ~ after the Binder or the ControlEvent property.**
```Swift
viewModel.text ~> label.rx.text
```
If anyone has a good idea about this, please contact me here https://github.com/RxSwiftCommunity/RxBinding/issues/1 or create a PR.
Thanks.
The operator `~>` is equal to `bind(to:)`.
```swift
viewModel.text ~> label.rx.text
```
is euqals to
```swift
viewModel.text.bind(to: label.rx.text)
````
I mean how to combine the method `disposed(by:)` into the operator `~>`.
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Author
lm2343635, lm2343635@126.com
## License
RxBinding is available under the MIT license. See the LICENSE file for more info.