0
votes

I've got a few list elements with some movie titles in them. When a user clicks on a list element containing a movie title I want to save that movie title into the Backbone collection so that the movie title is added to the users record.

This is my indexView

class Movieseat.Views.MovieseatsIndex extends Backbone.View

  template: JST['movieseats/index'] 

  initialize: -> 
    @collection.on('reset', @render, this)

  render: -> 
    $(@el).html(@template(movies: @collection))
    this

  events: -> 
    "click li": "showtext" 

  showtext: (e) -> 
    movie_title = $(e.target).text()
    @collection.create title: $('movie_title').text()

My Collections

class Movieseat.Collections.Movieseats extends Backbone.Collection

  url: '/api/movies'
  defaults: 
    title: "" 

And my template

<% for movie in @movies.models: %>
    <div class="movie-frame"><%= movie.get('title') %></div>
<% end %>

I had this working using backbone models, but I since then I've been following the RailsCast backbone video's and there he's using collections so I'm switching over.

On my homepage (where I render the template) I have a few .movie-frame elements filled with a movie title from when I was using models.

But when I click on a element now, I do get a new .movie-frame element, but it doesn't have any content. So I'm guessing the problem is within this part

  showtext: (e) -> 
    movie_title = $(e.target).text()
    @collection.create title: $('movie_title').text()

I've been looking in this code for to long and feel I'm missing something obvious.

1

1 Answers

0
votes

Sometimes you just need to take a step back, take a short walk and get some fresh air.

$('movie_title')

This isn't a variable anymore

$movie_title

This is. Now I've changed this the collection is saving the variable into the Collection.