Spine JS 使用 Hem 渲染 .eco 模板时出错:“无法读取属性‘长度’”未定义的”

发布于 2024-12-29 15:29:39 字数 936 浏览 2 评论 0原文

学习spine.js 我毫无问题地完成了两个教程,似乎是一个很棒的框架,但是这个简单的小问题让我发疯,因为我不知道我能做些什么来解决它......

根据我对变量@的理解list 应该可以通过 .eco 模板(由 hem 编译)访问,但事实并非如此,还有其他人遇到过这个吗?

有人可以告诉我哪里出错了吗?

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 %>

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 %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月无声 2025-01-05 15:29:39
@html require('views/UserApp')()

需要一个哈希对象作为参数。因此,如果你想在视图中使用 @list 变量(我的意思是 Rails),你必须执行如下操作:

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

其中键是视图中变量的名称。因此,使用:

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

就像您所做的那样,会将 @list 变量作为当前 @this 带到视图中,并且在您的视图中您应该能够通过以下方式使用它:

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

但它的可读性不那么好。

@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.

装纯掩盖桑 2025-01-05 15:29:39

我认为模板期望接收一个对象。然后,您可以使用 @key_name 访问该对象的属性;

尝试这样的事情(免责声明:我不知道 Coffeescript )

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文