10/28/17

Rxjs - Discovering Subject and Subject Behavior

In frameworks like angular and react we have this behavior that tracks every change of model (a javascript variable which stores state of some ui component) and immediately projects all the model changes to ui.
lets say we want to mimic this behavior without using any framewok.
For example: we have person object in our application that has a boolean "isMale" field (among other fields).

let person = {
  name:"Holden",
  lname:"Colfield",
  isMale:"true"
}

Lets say we need UI to react to change of this "isMale" field, and change checkbox caption accordingly
here is when rxjs can be useful:
One of the options to achieve the desired behavior with rxjs - is to define person object as Behavior Subject

let bSubject = new Rx.BehaviorSubject(person); 

Behavior subject is an observable sequence which makes you able to notify everyone of its subscribers when some change occurs.
Only thing you do is call "next" method of the subject:
(In our app we will trigger change when somebody cliks on the checkbox)

Rx.Observable.fromEvent(checkBox,'change').subscribe(x=> {
   person.isMale = x.target.checked;
   bSubject.next(person);
})

Now our app changes its ui according to changes of observed "person" object...
see running code

Getting started with docker

It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...