0

Several days ago I read about binding.scala and I found it so cool therefore I decided to write my own single page app.

The problem is that I'm trying to add "li" items into "ul" element, but it seems like the component Want doesn't see updates.

The code below:

case class Movie(title: Var[String], raring: Var[Int], watched: Var[Boolean])
var movies = Vars.empty[Movie]

@dom def Want = {
println(movies.bind, "!@#!@#!@#!")
<div class="want">
  <ul>
    {for (movie <- movies.value) yield {
    <li>
      <div>
        <span>
          {movie.title.bind}
        </span>
        <button onclick={event: Event => {
          event.preventDefault()
          movies.value.remove(movies.value.indexOf(movie))
          println(movies.value)
        }}></button>
      </div>
    </li>
  }}
  </ul>
</div>

When I change movies nothing happens.

UPDATE

After the comment below I updated the code:

def remove(movie:Movie) = {
    movies.value.-=(movie)}
@dom def Want = {
println(movies, "!@#!@#!@#!")
<div class="want">
  <ul>
    {for (movie <- movies.bind) yield {
    <li>
      <div>
        <span>
          {movie.title.bind}
        </span>
        <button onclick={event: Event => {
          event.preventDefault()
          remove(movie)
        }}></button>
      </div>
    </li>
  }}
  </ul>
</div>

} However, the code doesn't work.

  • I gave a downvote to your question, because your updated code is entirely in a mess. You will be more likely to get helped if you create a producible example on https://scalafiddle.io/ and link to it from this question. – Yang Bo Aug 24 '17 at 08:55

1 Answers1

2

Please change for (movie <- movies.value) to for (movie <- movies).

According to the Scaladoc of value method:

Note: This method must not be invoked inside a @dom method body.

Yang Bo
  • 3,586
  • 3
  • 22
  • 35