流星帮手未运行反应
问题描述:
我使用mizzao:user-status
包列出了在线用户。流星帮手未运行反应
请注意,下面的代码是coffeescript,它一直工作正常,特别是因为我坚持使用Blaze而不是React(我还没有找到在coffeescript中使用JSX的方法)。
除了切线,我特别在 client.coffee
有问题。 debugger
只被调用一次,当我检查其中的users_data
变量时,它显示一个空数组。很明显,至少有一部分是反应性的,因为当我继续经过断点并再次检查users_data
的值时,它是非空的。但users
帮手的返回值似乎没有改变。该功能不会重新运行。
TL;博士
如何使users
辅助方法,重新运行时users_data
改变
# ------------------
# server.coffee
# ------------------
Meteor.publish 'user_status', ->
Meteor.users.find(
{ "status.online": true },
fields: {}
)
# ------------------
# client.coffee
# ------------------
Meteor.subscribe 'online_users'
users = Template.users
users.helpers
users: ->
window.users_cursor = Meteor.users.find
"status.online": true
window.users_data = users_cursor.collection._docs._map
debugger
window.final = Object.keys(users_data).map (_id) =>
user = users_data[_id]
{
_id,
name: user.profile.name
}
final
和火焰模板的相关部分:
<body>
<h1>Welcome to Meteor!</h1>
{{> loginButtons }}
{{> users}}
</body>
<template name="users">
{{#each users}}
<li>{{_id}}</li>
{{/each}}
</template>
答
值得庆幸的是,这是不是Meteor本身的错误,也不是很难纠正。
这整个事情是没有必要的:
users: ->
window.users_cursor = Meteor.users.find
"status.online": true
window.users_data = users_cursor.collection._docs._map
debugger
window.final = Object.keys(users_data).map (_id) =>
user = users_data[_id]
{
_id,
name: user.profile.name
}
final
相反这就足以:
users: ->
Meteor.users.find({})
的Meteor.users.find
结果不是一个数组,并不[0]
风格索引,这就是为什么我不回应我认为它不适合作为帮手的回报价值。但它确实回应forEach
。这就是为什么它与模板的下列重构工作:
<template name="users">
{{#each user in users}}
<li>{{user._id}</li>
{{/each}}
</template>
剩下的一个问题是,Meteor.users.find("status.online": true)
不仍能正常工作,它必须是find({})
代替。我会研究这个问题,并可能发布一个关于它的问题。
答
除了maxple的答案,你也可以做到这一点在你的HTML代码:
<template name="users">
{{#each users}}
{{this._id}}
{{/each}}
</template>
更确切地说,它是周围的其他方式:['each'(HTTP:// blazejs .org/api/spacebars.html#Each)Blaze模板标签需要Meteor集合游标,但它也可以是普通数组。 – ghybs