所以我喜欢使用匿名函数进行编码,而Meteor.methods会为我打破这一点。所以,我创建了一个Meteor.Methods这样Meteor.Methods在一个函数中,可能的架构解决方案
//服务器端
Meteor.startup(function() {
// code to run on server at startup
//expose server methods.
Meteor.methods({
_SERVER_ : function(args){
try{
var funcStr = args.func.split("."); //split on the function parameter
var scopeStr = funcStr[0]; //get the scope of the function
funcStr.splice(0,1); //remove the scope and get the deep path
var path = funcStr.join("."); //join the array and stick it with "."
console.log("util.funcString("+ scopeStr +","+ path +")(" + args.data + ");");
if(myapp.hasOwnProperty(scopeStr)) //see if the function exist on myapp object
{
var scope = myapp[scopeStr]; //get the scope of the function
var response = util.funcString(scope, path)(args.data); //execute the function
console.log("myapp :" + args.func);
return response;
}else{
return "myapp don't have the method: " + args.func;
}
}catch(e){
return "myapp has a wtf moment and its saying:" + e;
}
}
});
});
所以相当多的功能,期待从客户端这样的电话。而其会打电话给myapp.page.add
//客户端
Meteor.call("_SERVER_",{
func : "pages.add",
data : page
},function(err, value){
insertNewPage(err,value);
});
的好处是现在我可以在服务器端这样创建一个函数。
//服务器端
myapp.page = (function(){
var privateVar = "private";
//private
function doSomething(){
}
//public via the return object
function add(){
console.log("called from client side");
}
return{
add : add
}
})();
而且我的应用程序现在它更像是模块化的,可在不同的文件和创建任何你想要的名称空间分开很简单。
问题DO我打破任何METEOR规则?它不是安全的?它是一个坏主意?任何建议都是值得欢迎的,我在Meteor还是个新手。
THANKS
你知道你可以多次调用Meteor.methods吗?所以你可以用'Meteor.methods({“pages.add”:function(){.... something ...}});' – stubailo 2014-10-21 23:40:42
复制上面的'我没有,这就是为什么我问。 – ncubica 2014-10-21 23:48:18
但仍然需要为每个函数声明Meteor.Methods中的方法。它不仅仅是一次呼吁在客户端统治他们。 – ncubica 2014-10-21 23:49:57