1

Learning spine.js I completed both the tutorials no problem, seems like a great framework, but this simple little problem is driving me nuts, because I have no idea what I can do to fix it...

From what I understand the variable @list should be accessible by the .eco template (compiled by hem), but it's not, has anybody else encountered this?

Can someone please show me where I am going wrong?

Users.coffee

Spine = require('spine')
User  = require('models/user')
$     = Spine.$

class Show extends Spine.Controller
  className: 'UserApp'

  events:
    'click .NewUser' : 'new'

  constructor: ->
    super    
    User.bind('create refresh change', @render)
    @active @render

  render: =>
    #get all users and render list
    @list= [1,2,3,4,5]
    console.log(@list)
    @html require('views/UserApp')(@list)

  new: ->
    @navigate('/users','create')

UserApp.eco

 <% if @list.length: %>
     <% for i in @list: %>
      <%= i %>
     <% end %>
    <% else: %>
     Why you no work!?
    <% end %>
just__matt
  • 484
  • 6
  • 15

2 Answers2

4
@html require('views/UserApp')()

expects an hash object as parameter. So, if you want to use a @list variable in your view (ala Rails I mean) you have to do something like the following:

@html require('views/UserApp')(list: @list)

where the key will be the name of your variable in the view. So using:

@html require('views/UserApp')(@list)

like you're doing will bring to the view the @list variable as the current @ or this and in your view you should be able to use it in the following way:

<% if @.length: %>
     <% for i in @: %>
      <%= i %>
     <% end %>
<% else: %>
     Why you no work!?
<% end %>

But it's not that readable.

lucapette
  • 20,564
  • 6
  • 65
  • 59
  • I was thinking of `@html require('views/UserApp')(@)` and `<% if @list.length %>` to limit the number of unadorned `@`s. I'm not familiar with Spine.js though so I don't know what the idiomatic approach would be. – mu is too short Jan 29 '12 at 18:59
  • I can't say what it could be considered idiomatic with Spine.js. Probably it's too young to have idioms. By the way, I tend to pass in hashed objects because that tends to make my views nicer. But I think it's a matter of taste. – lucapette Jan 29 '12 at 19:04
  • I prefer the explicit "hashed objects" approach as well, I suspect that Rails people would usually prefer to pass `@` and pretend that the view evaluates in the context of the caller. – mu is too short Jan 29 '12 at 19:09
3

I think the template expects to receive an object. Then you access a property of that object by using @key_name;

Try something like this ( Disclaimer: I don't know Coffeescript )

render: =>
    #get all users and render list
    @item = {}
    @item.list = [1,2,3,4,5]
    @html require('views/UserApp')(@item)
Kevin Ennis
  • 14,226
  • 2
  • 43
  • 44
  • 2
    AFAIK, you could also keep `@list = [...]` and pass just `@` to the template. See "Compiling Templates" in [the fine manual](http://spinejs.com/docs/views). – mu is too short Jan 29 '12 at 18:03
  • @muistooshort I thought that was probably a possibility, but my limited knowledge of Coffeescript prevented me from suggesting it. Thanks. – Kevin Ennis Jan 29 '12 at 18:07