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 accordinglyhere 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
No comments:
Post a Comment