2017-05-02 68 views
0

我使用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> 

回答

0

值得慶幸的是,這是不是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({})代替。我會研究這個問題,並可能發佈一個關於它的問題。

+0

更確切地說,它是周圍的其他方式:['each'(HTTP:// blazejs .org/api/spacebars.html#Each)Blaze模板標籤需要Meteor集合遊標,但它也可以是普通數組。 – ghybs

0

除了maxple的答案,你也可以做到這一點在你的HTML代碼:

<template name="users"> 
    {{#each users}} 
    {{this._id}} 
    {{/each}} 
</template>