0

In my template I have to put this.name and this.gravatar to access my user model data attributes. If I don't I get an error message when the template attempts to render. Everything works but I don't see any examples where they put this. in the templates.

My Backbone View:

Class MyApp.Views.Header extends Backbone.View
  template: JST['header']

  initialize: =>
    @model.on("change reset add", @render)

  render: =>
    @$el.html(@template (@model.attributes) )
    this

My Header Template:

<span id= "headerRight">
  <span>
    <a href="/classroom/help">Help</a>
  </span>
  <span> <img id="headerGravatar" src="<%= this.gravatar %>"></span>
  <span> <%= this.name %> </span>
  <span>
    <a class="logout" href='/signout'>Sign Out</a>
  </span>
</span>

How I generate my view:

headerView = new UCBCloudClassroom.Views.Header( model: @user)
    $('#header').html(headerView.render().el)
mu is too short
  • 426,620
  • 70
  • 833
  • 800
ChickenFur
  • 2,360
  • 3
  • 20
  • 26
  • Correct I am saying <%= this.name %> works but <%= name %> doesn't. I took out the space in the render method. Still no difference. I get the error message name is not defined when the template tries to render. If I just change the template code to this.name it works fine. – ChickenFur May 07 '13 at 18:46
  • Maybe it has something to do with using the rails asset pipeline and the template is in a jst.eco file. I get an error in the javascript console saying name is not defined – ChickenFur May 07 '13 at 20:06

1 Answers1

1

Backbone requires underscore so most of the examples you see will use underscore templates which have the <%= property %> syntax.

Eco templates use the <%= @property %> syntax for displaying model attributes link:

Since <%= @property %> is equivalent to <%= this.property %> everything is working as intended.

Scott Puleo
  • 3,684
  • 24
  • 23