2012-04-30 17 views
5

में एक Meteor.call फ़ंक्शन का परिणाम कैसे प्राप्त करें मैं एक उल्का क्लाइंट में उपयोग के लिए एक पेजिनेशन फ़ंक्शन बनाने की कोशिश कर रहा हूं। इसलिए मुझे सर्वर पर रिकॉर्ड गिनती जानने की आवश्यकता है।एक टेम्पलेट

सर्वर पर (सर्वर/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 से() रिटर्न

अपरिभाषित
आर 30

मैं समझता हूँ 'अनिर्धारित' Template.pager.RecordCount() से वापसी है और यह पहले दिया जाता है।

जब परिणाम उपलब्ध होता है तो यह कंसोल पर प्रदर्शित होता है।

लेकिन मैं अपने पेजर टेम्पलेट में परिणाम का मूल्य कैसे प्राप्त करूं?

मैं कुछ घंटों के लिए जावा कॉलबैक खोज रहा हूं, लेकिन जो कुछ भी मैं कोशिश करता हूं, मैं इसे काम नहीं कर सकता।
कृपया मदद करें।

यहां एक अद्यतन है।

मैंने अमान्य के लिए प्रलेखन को देखा। लेकिन उदाहरण मुझे बहुत मदद नहीं करता है। तापमान कॉल में एक पैरामीटर के साथ क्लाइंट में तापमान सेट किया गया है। तो कोई कॉलबैक इस्तेमाल नहीं किया जाता है। कॉलबैक मेरी समस्या थी।

मैं इसे इस तरह हल:

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

उत्तर

2

आप टेम्पलेट को अमान्य करने की जरूरत है, इस, अपने टेम्पलेट सहायक में सत्र का उपयोग कर संग्रह का उपयोग कर या का उपयोग करते हुए रद्द संदर्भ द्वारा किया जा सकता:

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

अद्यतन:

ईमानदारी से कहूं तो जैसा कि आप कहते हैं, आपके पास सही है, मैं बस सत्रों की संख्या को कम कर दूंगा। असल में टेम्पलेट को अमान्य करने के तीन तरीके हैं: context.invalidate() के साथ अमान्यता को बल दें, क्लाइंट संग्रह अद्यतन करें या सत्र अपडेट करें।

तो, हाँ आप (के रूप में मैं कॉफी स्क्रिप्ट का उपयोग नहीं करते Sudo गन्दा) इस कोड का उपयोग कर सकता है

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

व्यक्तिगत रूप से मैं कॉलबैक होने पर सही संदर्भ पर अमान्य कॉल करके समान चीजें कर रहा हूं। आप मूल रूप से एक ही चीजें कर रहे हैं लेकिन सत्रों के साथ। हालांकि मैं हेल्पर्स का उपयोग कर रहा हूं जो आपको HTML स्ट्रिंग्स वापस करने की अनुमति देता है लेकिन आप इसे अन्य तरीकों से कार्यान्वित कर सकते हैं। मैं ऊपर दिए गए मेरे उत्तर में एक कोड नमूना जोड़ूंगा। – jonathanKingston

+0

मुझे अमान्य का उपयोग कर कोड नमूना देखना अच्छा लगेगा। मेरे पास यह विचार है कि सत्र का उपयोग करके बहुत अधिक इंटरनेट क्रियाएं ट्रिगर होंगी जिन्हें शायद टाला जा सकता है। मेरे पेजर उदाहरण के लिए पूर्ण कोड github पर है: github.com/eric-naguras/MyApp.git –