Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在Meteor中分组方法吗?_Meteor - Fatal编程技术网

我可以在Meteor中分组方法吗?

我可以在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开始,想组织我的方法。。。 例如,我有两个集合:“a”和“b”,都有insert方法。。我想这样做:

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方法挂钩,它们将继续扩展