簡體   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