将外部函数返回的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
软件包,请记住不要订阅新的树。
相关文章:
- 我可以从内部函数中产生吗?
- 从内部函数javascript内部分配外部函数的对象
- 将外部函数返回的id传递给内部函数
- 重复调用的同一函数会重置setTimeout内部函数
- Javascript中的内部函数作用域
- 对象内部函数内的对象文本的范围
- 内部函数不会为外部函数在 jQuery 中动态创建的元素赋值
- JavaScript 如何定义内部函数
- bluebird promise catch() 没有使用 Promise.CancelError 调用内部函数
- JavaScript 在内部函数中保留一个 var
- 如何使用Javascript访问内部函数
- 使用从内部函数返回的异步数据对外部函数返回promise
- Javascript对象看不到内部函数
- 为什么可以在内部函数成员中访问对象引用,而不能在内部属性成员中访问
- 如何将参数传递到内部函数中
- JavaScriptEs6在内部函数中引用了这一点
- 需要将内部函数绑定到元素的单击事件
- 将此对象传递给JavaScript中的内部函数
- JavaScript 面向对象的调用函数内部函数或调用函数 insede var
- 从内部函数内部突破while循环'的身体