diff --git a/src/main/scala/scatan/lib/mvc/ScalaJSView.scala b/src/main/scala/scatan/lib/mvc/ScalaJSView.scala index 467a44b..38d523d 100644 --- a/src/main/scala/scatan/lib/mvc/ScalaJSView.scala +++ b/src/main/scala/scatan/lib/mvc/ScalaJSView.scala @@ -8,17 +8,12 @@ import org.scalajs.dom * The type of the state of the view. */ trait ScalaJSView[State <: Model.State]( - val container: String, // The id of the container element - val initialState: State // The initial state of the view + val container: String // The id of the container element ) extends View[State]: - private val _reactiveState = Var[State](initialState) - override def updateState(state: State): Unit = - _reactiveState.writer.onNext(state) - /** A signal that emits the current state of the application. */ - def reactiveState: Signal[State] = _reactiveState.signal + def reactiveState: Signal[State] /** The element that is rendered by this view. */ @@ -54,7 +49,14 @@ abstract class BaseScalaJSView[State <: Model.State, C <: Controller[State]]( container: String, requirements: View.Requirements[C] ) extends BaseView[State, C](requirements) - with ScalaJSView[State]( - container, - requirements.controller.state - ) + with ScalaJSView[State](container): + + private val _reactiveState = Var[State](controller.state) + + override def updateState(state: State): Unit = + super.updateState(state) + _reactiveState.writer.onNext(state) + + /** A signal that emits the current state of the application. + */ + def reactiveState: Signal[State] = _reactiveState.signal diff --git a/src/main/scala/scatan/lib/mvc/View.scala b/src/main/scala/scatan/lib/mvc/View.scala index 49adcb8..3fa5a2d 100644 --- a/src/main/scala/scatan/lib/mvc/View.scala +++ b/src/main/scala/scatan/lib/mvc/View.scala @@ -6,6 +6,10 @@ package scatan.lib.mvc */ trait View[State <: Model.State]: + /** The state of the application. + */ + def state: State + /** Displays the view. */ def show(): Unit @@ -57,4 +61,12 @@ trait NavigatorView extends View[?]: abstract class BaseView[State <: Model.State, C <: Controller[State]](requirements: View.Requirements[C]) extends View[State] with NavigatorView - with View.Dependencies(requirements) + with View.Dependencies(requirements): + + override def state: State = _state + + private var _state: State = controller.state + override private[mvc] def updateState(state: State): Unit = + _state = state + +