5

我正在尝试制作一个用于 Meteor 客户端的分页功能。因此我需要知道服务器上的记录数。

在服务器上(在 server/bootstrap.coffee 中)我有这个代码:

Meteor.methods
  ContactsCount: ->
    Contacts.find().count()
    console.log("Totalrecords: " + Contacts.find().count())

调用服务器部分(它在控制台上显示正确的数字 - 40)

在客户端我有:

$.extend Template.pager,
  GetRecordCount: ->
    Meteor.call("ContactsCount", (error,result) ->
    console.log('r', result)

从浏览器控制台 Template.pager.RecordCount() 返回

未定义
r 30

我知道“未定义”是 Template.pager.RecordCount() 的返回,它首先返回。

当结果可用时,它会显示在控制台上。

但是如何在我的寻呼机模板中获取结果的值?

我现在正在搜索 java 回调几个小时,但无论我尝试什么,我都无法让它工作。
请帮忙。

这是一个更新。

我查看了无效的文档。但是这个例子对我帮助不大。温度在客户端中通过函数调用中的参数设置。所以没有使用回调。回调是我的问题。

我是这样解决的:

Meteor.call("ContactsCount", myFunc)

### This is the call back function when the server
    function 'Meteor.call("ContactsCount", myFunc)' is called
    When the result from the server call is returned, this will be executed ###
myFunc = (error, result) ->
if !error
    pages = result / Session.get("page_size")
    Session.set "total_pages", Number(pages.toFixed(0) + 1)
    Session.set "total_records", result
if error
    console.log(error)

这行得通。我仍然想知道这是否是最好的解决方案。我有很多 Session.set() 调用,也许有太多的触发发生。

### This function will set the css classes
    for enabling or disabling the pager buttons
    in the Pager Template in myapp.html ###
SetPagerButtons = ->
 Meteor.call("ContactsCount", myFunc)
 if Session.get("current_page") <= 1
    Session.set "nextEnabled", ""
    Session.set "lastEnabled", ""
    Session.set "firstEnabled", "disabled"
    Session.set "previousEnabled", "disabled"
    Session.set "last_record", false
 else if Session.get("last_record") or Session.equals("current_page",  Session.get("total_pages"))
    Session.set "nextEnabled", "disabled"
    Session.set "lastEnabled", "disabled"
    Session.set "firstEnabled", ""
    Session.set "previousEnabled", ""
 else
    Session.set "nextEnabled", ""
    Session.set "lastEnabled", ""
    Session.set "firstEnabled", ""
    Session.set "previousEnabled", ""
    Session.set "last_record", false
4

1 回答 1

2

您需要使模板无效,这可以通过在模板助手中使用会话、使用集合或使用无效上下文来完成:

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

更新:

老实说,您所说的内容是正确的,我只是尽量减少会话次数。基本上有三种方法可以使模板失效:使用 context.invalidate() 强制失效,更新客户端集合或更新会话。

所以是的,您可以使用此代码(Sudo messy,因为我不使用咖啡脚本)

//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}}
于 2012-04-30T09:35:46.303 回答