我可以在Meteor中分组方法吗?
我从Meteor开始,想组织我的方法。。。 例如,我有两个集合:“a”和“b”,都有insert方法。。我想这样做:我可以在Meteor中分组方法吗?,meteor,Meteor,我从Meteor开始,想组织我的方法。。。 例如,我有两个集合:“a”和“b”,都有insert方法。。我想这样做: Meteor.methods({ a: { insert : function(){ console.log("insert in Collection a"); } }, b: { insert : function(){ console.log("inser
Meteor.methods({
a: {
insert : function(){
console.log("insert in Collection a");
}
},
b: {
insert : function(){
console.log("insert in Collection b");
}
}
});
然后致电Meteor.call('a.insert')代码> 有可能做到这一点吗?或者我如何组织我的方法
我不想让方法像:“insertA”和“insertB”您可以使用以下语法:
Meteor.methods({
"a.insert": function(){
console.log("insert in Collection a");
}
"b.insert": function(){
console.log("insert in Collection b");
}
});
它允许你做
Meteor.call(“a.insert”)代码>基于Saimemount的想法,如果您关心代码中这些组的优雅,还可以在代码中添加一个帮助函数。然后,您可以使用您喜欢的符号,甚至可以任意嵌套组:
var methods = {
a: {
insert : function(){
console.log("insert in Collection a");
}
},
b: {
insert : function(){
console.log("insert in Collection b");
},
b2: {
other2: function() {
console.log("other2");
},
other3: function() {
console.log("other3");
}
}
},
};
function flatten(x, prefix, agg) {
if (typeof(x) == "function") {
agg[prefix] = x;
} else {
// x is a (sub-)group
_.each(x, function(sub, name) {
flatten(sub, prefix + (prefix.length > 0 ? "." : "") + name, agg);
});
}
return agg;
}
Meteor.methods(flatten(methods, "", {}));
然后用点符号调用,如:
Meteor.call('b.b2.other2');
这是我们目前在Edthena使用的样式。我还看到a/insert
更像一个URL。这只是个人喜好的问题。我也是这么做的,很方便。你也可以有多个meteor方法挂钩,它们将继续扩展