1

I'm in the process of hacking/learning my way though backbone.js. For some reason I can follow the @account model instance all the way down to the view but in the template when I do <%= account.get('name') %> nothing is returned. When I do <%= account %> I get [object Object]

Am I simply using the wrong tag to display account?

Anyways, here's my code.

Router: shows.js.coffee:

class AppName.Routers.Shows extends Backbone.Router
  routes:
    '': 'index'

  initialize: ->
    # Fetch Show Dates
    @collection = new AppName.Collections.Shows
    @collection.fetch()
    # Fetch Account Info
    @account = new AppName.Models.Account
    @account.fetch()

  index: ->
    # Shows
    view = new AppName.Views.ShowsIndex(collection: @collection)
    $('div.shows_container').html(view.render().el)
    # Account
    view = new AppName.Views.Account(model: @account)
    $('div.account_container').html(view.render().el)

Model: show.js.coffee

class AppName.Models.Account extends Backbone.Model
  urlRoot: 'api/account'

View: show.js.cofee

class AppName.Views.Account extends Backbone.View
  template: JST['accounts/show']

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

Template: show.jst.ejs

<%= account.get('name') %>
matthew
  • 43
  • 3
  • I'm not so familiar with coffescript so I don't know the exact syntax, but I think you need to call [toJSON](http://documentcloud.github.com/backbone/#Model-toJSON) on your model when you pass it in to the template. Have a look at this question [backbone-js-not-picking-up-model-context](http://stackoverflow.com/questions/13496231/backbone-js-not-picking-up-model-context/13496675#13496675) – Jack Nov 21 '12 at 19:26
  • @Jack I actually tried that too. No luck. – matthew Nov 21 '12 at 19:36
  • What makes you think that `@account.fetch()` has gotten anything from the server when you try to `$(@el).html(@template(account: @model))`? – mu is too short Nov 21 '12 at 20:37
  • @muistooshort Adding `console.log(@model)` **after** `$(@el).html(@template(account: @model))` shows me a Account instance with the correct attributes: inside. So I know it's fetching correctly. – matthew Nov 21 '12 at 21:19
  • @muistooshort Here is a screenshot of the console http://i.imgur.com/QbDvU.png Maybe that will help. – matthew Nov 21 '12 at 21:22
  • Don't trust `console.log`, it keeps a live reference to what you're logging so it doesn't show you the state of things when you say `console.log(stuff)`: http://stackoverflow.com/a/11463190/479863, http://stackoverflow.com/a/10418979/479863, http://stackoverflow.com/a/10944246/479863, http://stackoverflow.com/a/12540237/479863, and probably a few others. You should bind your `render` to a `"change"` event on the model as that's what [`fetch`](http://backbonejs.org/#Model-fetch) will trigger when it gets something. – mu is too short Nov 21 '12 at 21:30
  • Things to remember: (1) `fetch` is an AJAX call (2) always render in response to events rather than assuming that things are ready (3) views should do something sensible with empty models/collections. Combine those and you can render views pretty much whenever you want and things should work. – mu is too short Nov 21 '12 at 23:18

0 Answers0