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

后端 未结 1 996
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 02:17

I\'m trying to make a pagination function for use in a Meteor client. Therefore I need to know the record count on the server.

On the server (in server/bootstrap.cof

相关标签:
1条回答
  • 2021-01-13 02:50

    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}}
    
    0 讨论(0)
提交回复
热议问题