Created
June 21, 2012 07:41
-
-
Save nachiket-p/2964422 to your computer and use it in GitHub Desktop.
Meteor: Calling server method from client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<title>meteor_servercall</title> | |
</head> | |
<body> | |
{{> simple}} | |
{{> passData}} | |
</body> | |
<template name="simple"> | |
<h1>Calling serve method</h1> | |
<input type="button" value="Call" /> | |
<div id="simpleResult">{{result}}</div> | |
</template> | |
<template name="passData"> | |
<h1>passing data to server method</h1> | |
<span>Name: </span><input type="text" value="Nachiket"/> | |
<input type="button" value="Call" /> | |
<div id="passDataResult">{{result}}</div> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Meteor.is_client) { | |
Template.simple.result = function () { | |
return Session.get('serverSimpleResponse') || ""; | |
}; | |
Template.simple.events = { | |
'click input' : function () { | |
Meteor.call('getCurrentTime',function(err, response) { | |
Session.set('serverSimpleResponse', response); | |
}); | |
} | |
}; | |
Template.passData.result = function () { | |
return Session.get('serverDataResponse') || ""; | |
}; | |
Template.passData.events = { | |
'click input[type=button]' : function () { | |
Meteor.call('welcome', $('input[type=text]').val(), function(err,response) { | |
if(err) { | |
Session.set('serverDataResponse', "Error:" + err.reason); | |
return; | |
} | |
Session.set('serverDataResponse', response); | |
}); | |
} | |
}; | |
} | |
if (Meteor.is_server) { | |
Meteor.startup(function () { | |
Meteor.methods({ | |
getCurrentTime: function () { | |
console.log('on server, getCurrentTime called'); | |
return new Date(); | |
}, | |
welcome: function (name) { | |
console.log('on server, welcome called with name: ', name); | |
if(name==undefined || name.length<=0) { | |
throw new Meteor.Error(404, "Please enter your name"); | |
} | |
return "Welcome " + name; | |
} | |
}); | |
}); | |
} |
This was helpful.
Nice!
Well done !
+1
+1, very helpful
Thanks. Quite helpful
Good, thanks 😄
Thanks for this post. I want to call a method that returns a pre-filled html form to the client. please how can I do this?
Thank you 4 years later ;)
Thanks
Update.
Now Meteor.isClient
, not Meteor.is_client
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for posting this!