cipacda cipacda - 1 year ago 145
Scala Question

Scala.js with angular type casting

I am trying to implement an application with Scala.JS and Angular and I am facing a problem with type casting when submitting the form.

I have the following code:

<div ng-controller="usersCtrl">
<form novalidate class="simple-form">
First Name: <input type="text" ng-model="user.firstName" /><br />
Last Name: <input type="text" ng-model="user.lastName" /><br />
E-mail: <input type="email" ng-model="" /><br />
Password: <input type="password" ng-model="user.password" /><br />
ConfirmPassword: <input type="password" ng-model="user.passwordConfirmation" /><br />
<input type="submit" ng-click="controller.signup(user)" value="Save" />

class UsersCtrl(
scope: UsersScope,
proxy: UsersServiceProxy,
val timeout: Timeout)
extends AbstractController[UsersScope](scope) {

def signup(user: UserSignup): Future[User] = {

case class UserSignup(firstName: String, lastName: String, email: String, password: String, passwordConfirmation: String)

But when I try to submit the form I get the following error:

$c_sjsr_UndefinedBehaviorError {s$1: "An undefined behavior was detected: [object Object] is not an instance of rentalot.users.UserSignup", e$1: $c_jl_ClassCastException

I have tried several options, including getting the user via the scope, with having a user variable defined either as an UserSignup or as UndefOf[UserSignup] but with no luck. The latter fails when I try to do scope.user.toOption.

Any thoughts?

Answer Source

Scala.JS can't automatically convert a JS Object to Scala case class. You need to define a facade. Refer to this document. It should look something like this:

trait UserSignup extends js.Object { 
  def firstName: String = js.native
  def lastName: String = js.native
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download