Skip to content

Commit

Permalink
observers implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Cedervall committed Oct 9, 2019
1 parent f67ab20 commit 9d68f76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Dishes/Dishes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class Dishes extends Component {
status: "LOADING",
dishes: null,
dishType: "All",
query: ""
query: "",
guests: modelInstance.getNumberOfGuests()
};

modelInstance.addObserver(this);
}

// this methods is called by React lifecycle when the
Expand All @@ -41,6 +44,13 @@ class Dishes extends Component {
});
}

update(details) {
console.log('updating from observer: ', details)
this.setState({
guests: modelInstance.getNumberOfGuests()
})
}

render() {
let dishesList = null;
const dishTypes = [
Expand Down Expand Up @@ -137,7 +147,9 @@ class Dishes extends Component {
};

return (

<div className="Dishes">
<p>Guests {this.state.guests}</p>
<div>
<div id="dishSearchViewWrapper">
<div id="sideBarView"></div>
Expand Down
2 changes: 2 additions & 0 deletions src/data/DinnerModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DinnerModel {
this.updateObservers("input-num-guests");
this.updateObservers("value-num-guests");
document.cookie = 'guests=' + num + ';';
this.updateObservers('guests');

}

Expand Down Expand Up @@ -197,6 +198,7 @@ class DinnerModel {
}
updateObservers(detailsToUpdateWith) {
// instead define update function in each observer that doesnt re-render entire view
console.log('observer cointains: ', this._observers)
this._observers.forEach(obs => obs.update(detailsToUpdateWith));
}
}
Expand Down

0 comments on commit 9d68f76

Please sign in to comment.