Gregg Hernandez <gregg@lucidchart.com>
golucid.co
@js.native
trait Observable[T] extends js.Object {
def map[U](selector: js.Function1[T,U]): Observable[U] = js.native
def map[U](selector: Selector[T,U]): Observable[U] = js.native
// TODO: what is the return type of subscribe?
def subscribe[U<:T](observer: Observer[U]): js.Dynamic = js.native
def subscribe[U<:T](onNext: js.Function1[U,Any],
onError: js.Function1[js.Dynamic,Any] = null,
onCompleted: js.Function0[Any] = null) = js.native
}
scala-js.org
goo.gl/WfRLFC
and undefined
// /app/shared/src/main/scala/Data.scala
// Compiled to Bytecode & JavaScript
case class Data(name: String, count: Int)
object Data {
def serialize(d: Data): String = ...
def unserialize(s: String): Data = ...
}
// /app/server/src/main/scala/Server.scala
// Compiled to Bytecode
object Server {
def increment(request: Request) = {
val data = Data.unserialize(request.body)
Ok(data.copy(count = data.count + 1))
}
}
// /app/client/src/main/scala/Client.scala
// Compiled to JavaScript
object Client extends JSApp {
def main(): Unit = {
val data = Data("foo", 10)
val result = http.post("/increment", Data.serialize(data))
println(result) // Data(foo,11)
}
}
// /app/shared/src/main/scala/Data.scala
// Compiled to Bytecode & JavaScript
case class Data(name: String, count: Int)
object Data {
def serialize(d: Data): String = ...
def unserialize(s: String): Data = ...
}
// /app/server/src/main/scala/Server.scala
// Compiled to Bytecode
object Server {
def increment(request: Request) = {
val data = Data.unserialize(request.body)
Ok(data.copy(count = data.count + 1))
}
}
// /app/client/src/main/scala/Client.scala
// Compiled to JavaScript
object Client extends JSApp {
def main(): Unit = {
val data = Data("foo", 10)
val result = http.post(
"/increment",
Data.serialize(data))
println(result) // Data(foo,11)
}
}
@link class UserLink
case class UserLink(uri: URI)
object UserLink extends
LinkCompanion[UserLink]
The code you write:
Compiler output:
case class Foo(
bar: String,
baz: Int
)
val format = Json.format[Foo]
case class Foo(
bar: String,
baz: Int
)
val format = new Format[Foo] {
def reads(js: JsValue): JsResult[Foo] = {
for {
bar <- (js \ "bar").validate[String]
baz <- (js \ "baz").validate[Int]
} yield Foo(bar, baz)
}
def writes(foo: Foo): JsValue = {
Json.obj(
"bar" -> foo.bar,
"baz" -> foo.baz
)
}
}
https://angular.io/docs/ts/latest/tutorial/
import org.scalajs.dom
@Component(...)
class HeroDetailComponent(...) extends OnInit with OnDestroy {
...
def goBack(): Unit = {
dom.window.history.back()
}
...
}
@Component({
selector: 'my-app',
styleUrls: ['app/app.component.css'],
template: 'app/app.component.html',
providers: [HeroService]
})
export class AppComponent {
title = 'Tour of Heroes';
}
@Component(
selector = "my-app",
styleUrls = js.Array("app/app.component.css"),
templateUrl = "app/app.component.html",
providers = @@[HeroService]
)
class AppComponent {
val title = "Tour of Heroes"
}
@Component(
selector = "my-app",
styleUrls = js.Array("app/app.component.css"),
templateUrl = "app/app.component.html",
directives = @@[ROUTER_DIRECTIVES],
providers = @@[HeroService]
)
class AppComponent {
val title = "Tour of Heroes"
}
@Component(
selector = "my-app",
styleUrls = js.Array("app/app.component.css"),
templateUrl = "app/app.component.html",
directives = @@[ROUTER_DIRECTIVES],
providers = @@[HeroService]
)
class AppComponent {
val title = "Tour of Heroes"
}
@Component({
selector: 'my-dashboard',
templateUrl: 'app/dashboard.component.html',
styleUrls: ['app/dashboard.component.css']
})
export class DashboardComponent
implements OnInit {
heroes: Hero[] = [];
constructor(
private router: Router,
private heroService: HeroService) {
}
ngOnInit() {
this.heroService.getHeroes()
.then(heroes =>
this.heroes = heroes.slice(1, 5));
}
gotoDetail(hero: Hero) {
let link = ['/detail', hero.id];
this.router.navigate(link);
}
}
@Component(
selector = "my-dashboard",
templateUrl = "assets/dashboard.component.html",
styleUrls = js.Array("assets/dashboard.component.css")
)
class DashboardComponent(
router: Router,
heroService: HeroService
) extends OnInit {
var heroes = js.Array[Hero]()
def ngOnInit(): Unit = {
heroService.getHeroes().map { hs =>
heroes = hs.drop(1).take(4)
}
}
def gotoDetail(hero: Hero): Unit = {
router.navigate(js.Array[js.Any](
"detail", hero.id
))
}
}
@Component(
selector = "my-dashboard",
templateUrl = "assets/dashboard.component.html",
styleUrls = js.Array("assets/dashboard.component.css")
)
class DashboardComponent(
router: Router,
heroService: HeroService
) extends OnInit {
var heroes = js.Array[Hero]()
def ngOnInit(): Unit = {
heroService.getHeroes().map { hs =>
heroes = hs.drop(1).take(4)
}
}
def gotoDetail(hero: Hero): Unit = {
router.navigate(js.Array[js.Any](
"detail", hero.id
))
}
}
// from previous slide
hs.drop(1).take(4)
// extends to many other collection types
List(1, 2, 3, 4, 5, 6, 7).drop(1).take(4)
// List(2,3,4,5)
Map("one" -> 1, "two" -> 2, "three" -> 3).drop(1).take(1)
// Map("two" -> 2)
// infinite streams
val primes = {
val s = Stream.from(2)
def sieve(s: Stream[Int]): Stream[Int] =
s.head #:: sieve(s.tail.filter(_ % s.head != 0))
sieve(s)
}
primes.take(5).toList
// List(2,3,5,7,11)
primes.take(10).toList
// List(2,3,5,7,11,13,17,19,23,29)
primes.drop(100).take(5).toList
// List(547, 557, 563, 569, 571)
@Injectable()
export class HeroService {
getHeroes() {
return Promise.resolve(HEROES);
}
getHero(id: number) {
return this.getHeroes()
.then(heroes =>
heroes.find(hero => hero.id === id));
}
}
@Injectable()
class HeroService() {
def getHeroes(): Future[js.Array[Hero]] = {
Future.successful(Heroes.all)
}
def getHero(id: Int): Future[Option[Hero]] = {
getHeroes().map { heroes =>
heroes.find(_.id == id)
}
}
}
@Component(...)
export class HeroDetailComponent implements OnInit, OnDestroy {
hero: Hero;
sub: any;
constructor(private heroService: HeroService, private route: ActivatedRoute) {
}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = +params['id'];
this.heroService.getHero(id)
.then(hero => this.hero = hero);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
goBack() {
window.history.back();
}
}
@Component(...)
class HeroDetailComponent(heroService: HeroService, route: ActivatedRoute) extends OnInit with OnDestroy {
var hero: js.UndefOr[Hero] = js.undefined
var sub: js.UndefOr[js.Dynamic] = js.undefined
def goBack(): Unit = {
dom.window.history.back()
}
def ngOnInit(): Unit = {
sub = route.params.subscribe { params =>
val id = params.selectDynamic("id").asInstanceOf[String].toInt
heroService.getHero(id).map { hero =>
hero.map(this.hero = _)
}
}
}
def ngOnDestroy(): Unit = {
sub.map(_.unsubscribe())
}
}
var hero: js.UndefOr[Hero] = js.undefined
println(hero.name)
// Compilation Failed
// value name is not a member of js.UndefOr[Hero]
// println(hero.name)
// ^
var hero: js.UndefOr[Hero] = js.undefined
hero.map { hero =>
// this code is never executed
println(hero.name)
}
var hero: js.UndefOr[Hero] = Hero(11, "Mr. Nice")
hero.map { hero =>
println(hero.name) // prints: "Mr. Nice"
}
@Component(...)
class HeroDetailComponent(heroService: HeroService, route: ActivatedRoute) extends OnInit with OnDestroy {
var hero: js.UndefOr[Hero] = js.undefined
var sub: js.UndefOr[js.Dynamic] = js.undefined
def goBack(): Unit = {
dom.window.history.back()
}
def ngOnInit(): Unit = {
sub = route.params.subscribe { params =>
val id = params.selectDynamic("id").asInstanceOf[String].toInt
heroService.getHero(id).map { hero =>
hero.map(this.hero = _)
}
}
}
def ngOnDestroy(): Unit = {
sub.map(_.unsubscribe())
}
}
@Component(...)
class HeroDetailComponent(heroService: HeroService, route: ActivatedRoute) extends OnInit with OnDestroy {
var hero: js.UndefOr[Hero] = js.undefined
var sub: js.UndefOr[js.Dynamic] = js.undefined
def ngOnDestroy(): Unit = {
sub.map(dynSub => dynSub.unsubscribe())
}
def goBack(): Unit = {
dom.window.history.back()
}
def ngOnInit(): Unit = {
sub = route.params.subscribe { params =>
val id = params.selectDynamic("id").asInstanceOf[String].toInt
heroService.getHero(id).map { hero =>
hero.map(this.hero = _)
}
}
}
}
@Component(...)
class HeroDetailComponent(heroService: HeroService, route: ActivatedRoute) extends OnInit with OnDestroy {
var hero: js.UndefOr[Hero] = js.undefined
var sub: js.UndefOr[js.Dynamic] = js.undefined
def goBack(): Unit = {
dom.window.history.back()
}
def ngOnInit(): Unit = {
sub = route.params.subscribe { params =>
val id = params.selectDynamic("id").asInstanceOf[String].toInt
heroService.getHero(id).map { hero =>
hero.map(this.hero = _)
}
}
}
def ngOnDestroy(): Unit = {
sub.map(dynSub => dynSub.unsubscribe())
}
}
val id = params.selectDynamic("id").asInstanceOf[String].toInt
const routes: RouterConfig = [
{path: '', redirectTo: '/dashboard', pathMatch: 'full'},
{path: 'dashboard', component: DashboardComponent},
{path: 'detail/:id', component: HeroDetailComponent},
{path: 'heroes', component: HeroesComponent}
];
export const appRouterProviders = RouterModule.forRoot(routes);
object Routes {
val routes = RouterConfig(
RDef(path = "", redirectTo = "dashboard", pathMatch = "full"),
RDef(path = "heroes", component = @#[HeroesComponent]),
RDef(path = "dashboard", component = @#[DashboardComponent]),
RDef(path = "detail/:id", component = @#[HeroDetailComponent])
)
@JSExport
val routing = RouterModule.forRoot(routes)
}
object Routes {
val routes: RouterConfig = RouterConfig(
RDef(path = "", redirectTo = "dashboard", pathMatch = "full"),
RDef(path = "heroes", component = @#[HeroesComponent]),
RDef(path = "dashboard", component = @#[DashboardComponent]),
RDef(path = "detail/:id", component = @#[HeroDetailComponent])
)
@JSExport
val routing = RouterModule.forRoot(routes);
}
@Component(
inputs = js.Array("the-hero")
)
class HeroComponent
"the-hero" is an invalid JS identifier
var heroes: js.Array[Hero] = js.Array()
def ngOnInit(): Unit = {
heroService.getHeroes().map { hs =>
heroes = hs.drop(1).take(4)
}
}
var heroes: js.Array[Hero] = ngOnInit {
heroService.getHeroes().map { hs =>
heroes = hs.drop(1).take(4)
}
}
// nothing here now, at compile time
// the above code is rewritten into
// the common form
def ngOnInit(): Unit = {
}
github.com/gregghz/angulate2-play-example
github.com/jokade/angulate2
scala-js.org
Other sessions you should check out:
Primer to WebGL - Paul Draper - 4:10pm - Auditorium
Where's my JavaScript? A Close Look at Loading Up Your Application - Matt Swensen - 4:40pm - Room B