从template.onRendered访问Meteor.users

时间:2015-05-13 18:17:11

标签: meteor

我需要执行这段代码

Template.addPost.onRendered ->
@$('#name').val(Meteor.user().profile.name)

但似乎模板的呈现时间早于客户端上填充的Users collection。因此回到我身边

Cannot read property 'profile' of undefined

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您可以通过autorun

等待用户加载
function setUpPageElements () {
    $.each($('.collapsible'), function (index,value) { 
        value.collapsible({
          accordion : true
        });
    })
};

我们还利用coffeescript的存在运算符来安全地提取名称值。在javascript中,这个解决方案看起来像这样:

<ul class="collapsible" data-collapsible"accordion">
        <% if jobs.first %>
            <% jobs.each do |job| %>
                <%= render partial: 'job', locals: {job: job} %>
            <% end %> 
        <% else %>
        <li >
            <section id="header" class="collapsible-header active" > <p id="name">No active jobs</p></section>
            <section  class="collapsible-body">
                There are currently no active jobs.
                You can create new jobs by creating selecting 'New Job from navigation bar along the top'
            </section>
        </li>
        <% end %>
</ul>