Code Monkey home page Code Monkey logo

rxbinding's Introduction

RxBinding

CI Status Version License Platform swift

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.

Documentation

RxBinding is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxBinding'

With @_expoerted import, the operators can be used in the all file of the project.

@_exported import RxBinding

Usage of ~>

The type of text is Observable<String?> and the type of label.rx.text is Binder<String?>. RxSwfit provides the following method for the one way data binding between them.

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.

viewModel.text ~> label.rx.text ~ disposeBag

Bind an observable object to multiple binders.

viewModel.text ~> [label1, label2].map { $0.rx.text } ~ disposeBag

Usage of <~>

The type of text is BehaviorRelay<String?> and the type of textFeild.rx.text is ControlProperty<String?>. To apply the two way data binding between them, we need the following code by RxSwift.

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.

viewModel.text <~> textFeild.rx.text ~ disposeBag

Multiple Bindings

RxBinding supports using a single disposeBag for multiple binding operators like this:

disposeBag ~ [
    viewModel.text <~> textFeild.rx.text,
    viewModel.uppercaseText ~> label.rx.text,
    viewModel.charactersCount ~> [characterCountLabel1, characterCountLabel2].map { $0.rx.text }
]

or this:

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.

viewModel.text ~> label.rx.text

If anyone has a good idea about this, please contact me here #1 or create a PR. Thanks.

The operator ~> is equal to bind(to:).

viewModel.text ~> label.rx.text

is euqals to

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, [email protected]

License

RxBinding is available under the MIT license. See the LICENSE file for more info.

rxbinding's People

Contributors

lm2343635 avatar netspencer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rxbinding's Issues

Add operator `<~`

There is already an operator ~> what bind observable to observer.

How about add an operator <~ what bind observer by observable?

Possible to remove ~

I am considering how to remove the operator ~ after the Binder or the ControlEvent property.

viewModel.text ~> label.rx.text

If anyone has a good idea about this, please contact me here or create a PR.
Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.