Binding.scala使用教程14--自定义全局路由

Binding.scala使用教程14--自定义全局路由_第1张图片

Home
package app

import com.thoughtworks.binding.dom

object Home
{
  @dom def render =
  {
    

i am the HomePage

} }
Message
package app

import com.thoughtworks.binding.dom

object Message
{
  @dom def render =
  {
    

i am the MessagePage

} }
Friend
package app

import com.thoughtworks.binding.dom

object Friend
{
  @dom def render =
  {
    

i am the FriendPage

} }
Route

缺省值要写成这样 不然会导致返回类型不一致

case _ => 
package app

import com.thoughtworks.binding.Binding.{SingleMountPoint, Var}
import com.thoughtworks.binding.dom

object Route
{
  val path = Var("/home")
  @dom def render =
  {
    
{ path.bind match { case "/home" => Home.render.bind case "/message" => Message.render.bind case "/friend" => Friend.render.bind case _ =>
} }
} }
View
package app

import com.thoughtworks.binding.Binding.{ Vars}
import com.thoughtworks.binding.dom

import org.scalajs.dom.raw.{Event}


object View
{
  private val activeCode = Vars.empty[String]

  private val array=Array.fill(3)("green item")

  activeCode.value++=array

  private def handleClick(index: Int,path:String) =
  {
    for (i <- 0 until activeCode.value.length)
    {
        if(index==i)
          activeCode.value(i)="active green item"
        else
          activeCode.value(i)="green item"
    }
    Route.path.value=path
  }

  @dom def render =
  {

    
  }
}
MainApp
package app

import com.thoughtworks.binding.dom
import org.scalajs.dom.document

import scala.scalajs.js.JSApp

object MainApp extends JSApp
{
  def main(): Unit =
  {
    dom.render(document.body, View.render)
  }
}

防止IDEA 报错
import com.thoughtworks.binding.Binding

package object app
{
  implicit def makeIntellijHappy[T<:org.scalajs.dom.raw.Node](x: scala.xml.Node): Binding[T] =
    throw new AssertionError("This should never execute.")
}

你可能感兴趣的:(Binding.scala使用教程14--自定义全局路由)