1
votes

I am using gem 'backbone-on-rails' #https://github.com/meleyal/backbone-on-rails and I am trying to create a "new quote" view, with a blank form. Should be very simple. :S But all I get is this error:

Uncaught ReferenceError: description is not defined By debugging the error appears to happen on the View->render method.

This is the code:

Router:

class SpencerGrafica.Routers.Quotes extends Backbone.Router
    routes:
        'new'       : 'newQuote'

    newQuote: ->
        new SpencerGrafica.Views.NewQuote model: new SpencerGrafica.Models.Quote

View:

class SpencerGrafica.Views.NewQuote extends Backbone.View
  el: '#app'
  template: JST["quotes/new"]

  initialize: ->
    @render()

  render: ->
    $(@el).html(@template(@model.toJSON()))
    @

Model

class SpencerGrafica.Models.Quote extends Backbone.Model

  defaults:
    description: null

Template

<form id="new-quote" name="quote">

  <div>
    <input type="text" name="description" value="<%= description %>" placeholder="Descripcion interna">
  </div>

  <div class="actions">
    <input type="submit" value="Add Post" />
  </div>

</form>

Any help REALLY appreciated. :)

1
Can you test your template outside Backbone with a simple JSON object? I can't see any other reason this error would pop up. - Loamhoof
You are passing in model.toJSON() to the template, but in the template you are looking for a variable named description. It's likely that what you want in the template is actually model.description. Try that and see if it works. If that's not the issue make sure that description is something that exists on your model. - DigTheDoug
@DigTheDoug: The @model.toJSON() should be returning {description: null} unless something (such as backbone-on-rails) has changed the default toJSON behavior; Rails would want to see {quote: { description: ... }} so I'd look at what backbone-on-rails is doing. So I wonder what console.log(@model.toJSON()) has to say inside render. - mu is too short
By adding console.log(@model.toJSON()) like this: render: -> console.log(@model.toJSON()) $(@el).html(@template(quote: @model)) I get this: Object {description: null} - tomasbarrios

1 Answers

0
votes

try

class SpencerGrafica.Views.NewQuote extends Backbone.View
  el: '#app'
  template: JST["quotes/new"]

  initialize: ->
    @render()

  render: =>  # fat arrow here
    $(@el).html(@template(@model.toJSON()))