繁体   English   中英

将外部函数返回的id传递给内部函数

[英]Passing id returned by outer function to inner function

我在事件处理程序中有一个嵌套函数。 外部函数从Trees集合创建文档的副本。 然后,内部函数将创建来自Branches集合的任何文档的副本,其ID包含在原始树文档的treeBranches字段的数组中。

我需要将newTreeId从外部函数传递到内部函数,以便可以将新的分支ID添加到新文档的数组中。 内部函数中的console.log(newTreeID)当前返回未定义。

Template.Actions.events({
    'change .action-selection': function(e) {
        e.preventDefault();
        var selection = $(e.target).val();
        var currentTreeId = this._id;
        var branches = Branches.find({_id:{$in:this.treeBranches}});

        switch(selection) {
            case "repeat":
                return Meteor.call('treeRepeat', currentTreeId, function (newTreeId) {
                    branches.forEach(function(b) {
                        var currentBranchId = b._id;
                        console.log(newTreeId);
                        Meteor.call('treeBranchesRepeat', currentBranchId, newTreeId, function () {
                        });
                    });
                });
                break;
                ...

Meteor.methods({
    treeRepeat: function(currentTreeId) {
        check(currentTreeId, String);

        var tree = Trees.findOne({_id:currentTreeId}, {fields:{_id:0, treeBranches:0}});
        var newTreeId = Trees.insert(tree);

        return {
            _id: newTreeId
        };
    },
    treeBranchesRepeat: function(currentBranchId, newTreeId) {
        check(currentBranchId, String);
        check(newTreeId, String);

        var branch = Branches.findOne({_id:currentBranchId}, {fields: {_id: 0}});
        var newBranchId = Branches.insert(branch);
        Trees.update({_id:newTreeId},{$push:{treeBranches:newBranchId}});

        return {
            _id: newBranchId
        };
    }
});

我认为可以通过对服务器的单个请求执行树和分支的所有重复来简化此操作。 建议以下工作流程(未经测试,但我认为您明白了):

Template.Actions.events({
    'change .action-selection': function(e) {
        e.preventDefault();
        var selection = $(e.target).val();
        var currentTreeId = this._id;
        var currentTreebranches = this.treeBranches;

        switch(selection) {
            case "repeat":
                return Meteor.call('treeAllRepeat', currentTreeId, currentTreebranches, function (err, res) {
                  if (err) { console.log(err); }

                  console.log(res); // Your new tree _id
                });
                break;
                ...

Meteor.methods({
    treeAllRepeat: function(currentTreeId, branchIds) {
        check(currentTreeId, String);
        check(branchIds, [String]);

        var tree = Trees.findOne({ _id: currentTreeId }, { fields: { _id: 0, treeBranches: 0 } });

        if (!tree) {
          throw new Error('Tree with id ' + currentTreeId + ' not found');
        }

        var newTreeId = Trees.insert(tree);
        var branches = Branches.find({ _id: { $in: branchIds || [] } });

        branches.forEach(function (branch) {
          var newBranch = Branches.findOne({ _id: branch._id }, { fields: { _id: 0 } });
          var newBranchId = Branches.insert(newBranch);
          Trees.update({ _id: newTreeId }, { $push: { treeBranches: newBranchId } });
        });

        return {
          _id: newTreeId
        };
    }
});

我假设this.treeBranches是分支的ID? 如果没有,您可能想要执行以下操作:

var currentTreebranches = _.pluck(this.treeBranches, '_id');

PS。 如果您删除了autopublish包,请记住订阅新树。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM