How to get the result of a Meteor.call function in a template

心不动则不痛 提交于 2019-12-01 05:56:07

You need to invalidate the template, this can be done by using sessions in your template helper, using collections or using the invalidate context:

http://docs.meteor.com/#invalidate

Update:

To be honest what you have is correct as you say, I would just minimise the number of sessions. Basically there are three ways to invalidate a template: force an invalidation with context.invalidate(), Update a client collection or update a session.

So yeah you could use this code (Sudo messy as I don't use coffee script)

//client server call
total_records = 0
page_numbers_context = null

Meteor.call("ContactsCount", contactsCountCallback)

contactsCountCallback = (error, result) ->
if !error
    total_records = result
    if page_numbers_context
        page_numbers_context.invalidate();
if error
    console.log(error)



//Add template handler
Handlebars.registerHelper('page_numbers', pageNumberCallback);
pageNumberCallback = (options)  ->
    page_numbers 

    var context = Meteor.deps.Context.current;
    if context && !page_numbers_context
        page_numbers_context = context
        context.on_invalidate ->
            page_numbers_context = null

    pages = total_records / page_size
    total_pages = Number(pages.toFixed(0) + 1)
    //HTML code built with ifs here


//In template:
{{#page_numbers}}{{/page_numbers}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!