में एक 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
कृपया मेरे सवाल का अद्यतन को देखो। –
व्यक्तिगत रूप से मैं कॉलबैक होने पर सही संदर्भ पर अमान्य कॉल करके समान चीजें कर रहा हूं। आप मूल रूप से एक ही चीजें कर रहे हैं लेकिन सत्रों के साथ। हालांकि मैं हेल्पर्स का उपयोग कर रहा हूं जो आपको HTML स्ट्रिंग्स वापस करने की अनुमति देता है लेकिन आप इसे अन्य तरीकों से कार्यान्वित कर सकते हैं। मैं ऊपर दिए गए मेरे उत्तर में एक कोड नमूना जोड़ूंगा। – jonathanKingston
मुझे अमान्य का उपयोग कर कोड नमूना देखना अच्छा लगेगा। मेरे पास यह विचार है कि सत्र का उपयोग करके बहुत अधिक इंटरनेट क्रियाएं ट्रिगर होंगी जिन्हें शायद टाला जा सकता है। मेरे पेजर उदाहरण के लिए पूर्ण कोड github पर है: github.com/eric-naguras/MyApp.git –