Last active
December 18, 2015 17:29
-
-
Save jamgold/5819191 to your computer and use it in GitHub Desktop.
Meteor.call and stub methods
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.isClient) { | |
Template.hello.created = function() { | |
// this = Template Object | |
console.log("Template.hello.created"); | |
this.stub_method_result = 'not yet'; | |
}; | |
Template.hello.greeting = function () { | |
// this = Window Object | |
var v = Session.get("greeting"); | |
console.log("Template.hello.greeting "+v); | |
return v; | |
}; | |
Template.hello.rendered = function() { | |
// this = Template Object | |
console.log("Template.hello.rendered "+this.stub_method_result); | |
}; | |
Template.hello.events({ | |
'click input' : function (e, template) { | |
Meteor.call('stub_method', new Date().getTime() , function(err, result){ | |
template.stub_method_result = result; | |
Session.set("greeting", result); | |
}); | |
} | |
}); | |
Meteor.startup(function(){ | |
Session.setDefault("greeting", "Hello Default"); | |
}); | |
} | |
if (Meteor.isServer) { | |
Meteor.startup(function () { | |
// code to run on server at startup | |
}); | |
} | |
// | |
// this exists on server + client | |
// | |
Meteor.methods({ | |
stub_method: function(v) { | |
if(this.isSimulation) | |
{ | |
// this runs only on the client | |
console.log("stub_method method client "+v); | |
// never a return value, will always be async, so we need to use a Session value to get to the result | |
Session.set("greeting", "Hello Client "+v); | |
} | |
else | |
{ | |
// this only runs on the server | |
console.log("server "+v); | |
// create small delay before returning so we can see the client's response first | |
for(var i=parseInt(v,10);i>0;i--) { | |
i-=5000; | |
} | |
// the return value will be in the result parameter of the async callback | |
return "Hello Server "+v; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment