如何获取模板中Meteor.call函数的结果 [英] How to get the result of a Meteor.call function in a template

查看:157
本文介绍了如何获取模板中Meteor.call函数的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个分页函数用于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()返回


undefined

r 30


我理解'undefined'是Template.pager.RecordCount()的返回



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



我正在搜索java回调几个小时, ,我无法让它工作。

请帮助。



这是更新。



我查看了无效的文档
但是这个例子没有帮助我在客户端设置了一个参数在函数调用,所以没有回调使用。回调是我的问题。



我这样解决:

 调用(ContactsCount,myFunc)

###这是当服务器
函数'Meteor.call(ContactsCount,myFunc)'被调用时的回调函数
当返回服务器调用的结果时,将执行此命令###
myFunc =(error,result) - >
if!error
pages = result / Session.get(page_size)
Session.settotal_pages,Number(pages.toFixed(0)+ 1)
Session .settotal_records,result
如果错误
console.log(错误)

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

  ##此函数将设置css类
以启用或禁用寻呼机按钮
在pager模板中的myapp.html ###
SetPagerButtons = - >
Meteor.call(ContactsCount,myFunc)
如果Session.get(current_page)<= 1
Session.setnextEnabled,
Session。设置lastEnabled,
Session.setfirstEnabled,disabled
Session.setpreviousEnabled,disabled
Session.setlast_record,false
else if Session.get(last_record)或Session.equals(current_page,Session.get(total_pages))
Session.setnextEnabled,disabled
Session.set lastEnabled,disabled
Session.setfirstEnabled,
Session.setpreviousEnabled,
else
Session.setnextEnabled
Session.setlastEnabled,
Session.setfirstEnabled,
Session.setpreviousEnabled,
Session.setlast_record ,false


解决方案

您需要使模板无效,通过使用模板帮助器中的会话,使用集合或使用无效上下文来完成:

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

更新:



说实话,你所拥有的是正确的,你说,我只是最小化会话的数量。基本上有三种方法使模板无效:使用context.invalidate()强制失效,更新客户端集合或更新会话。



所以,你可以使用代码(我不使用咖啡脚本的Sudo乱码)

  //客户端服务器调用
total_records = 0
page_numbers_context = null

Meteor.call(ContactsCount,contactsCountCallback)

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



//添加模板处理程序
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)
//使用ifs创建的HTML代码


//在模板中:
{{#page_numbers}} {{/ page_numbers}}


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.coffee) I have this code:

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

The server part is called (it displays the correct number on the console - 40)

On the client I have:

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

From the browser console Template.pager.RecordCount() returns

undefined
r 30

I understand the 'undefined' is the return from Template.pager.RecordCount() and it is returned first.

When the result comes available it is displayed to the console.

But how do I get the value of result in my pager template?

I'm searching java callbacks for a few hours now, but whatever I try, I cant get it to work.
Please help.

Here is an update.

I looked at the documentation for invalidate. But the example doesn't help me much. The temperature is set in the client with a paramater in the function call. So there is no callback used. The callback was my problem.

I solved it like this:

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)

This works. I'm still wondering if this is the best solution. I have a lot of Session.set() calls and maybe there is too much triggering going on.

### 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

解决方案

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}}

这篇关于如何获取模板中Meteor.call函数的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆