Angularjs Firebase-查询链
所以我这里有一个有趣的问题。我们的软件包含一项功能,允许根据销售代表、经理之间的层次结构以及用户在使用我们的软件时希望支持的多个层次进行动态佣金分配。用户可能如下所示:Angularjs Firebase-查询链,angularjs,node.js,firebase,firebase-realtime-database,Angularjs,Node.js,Firebase,Firebase Realtime Database,所以我这里有一个有趣的问题。我们的软件包含一项功能,允许根据销售代表、经理之间的层次结构以及用户在使用我们的软件时希望支持的多个层次进行动态佣金分配。用户可能如下所示: $uid fname lname lineage fatherlink $uid_of_another_user childlink $uid_of_another_user $u
$uid
fname
lname
lineage
fatherlink
$uid_of_another_user
childlink
$uid_of_another_user
$uid_of_another_user
好的,现在让我们假设childlink
对象中的每个子对象都包含两个以上的子对象。例如,我希望能够从一个经理开始,创建一个包含他所有孩子的数组,以及他的孩子的孩子等等。。。由于我们支持任意多个管理级别,因此对给定用户下所有子级的查询需要是动态的。这是我们现在拥有的
var children = [];
_.child('user/' + $localStorage.currentUser.uid + '/lineage/child_link').on('value', function(sn1) {
sn1.forEach(function(sn2) {
_.child('user/' + sn2.key + '/lineage/child_link/').on('value', function(sn3) {
sn3.forEach(function(sn4) {
_.child('user/' + sn4.key + '/lineage/child_link/').on('value', function(sn5) {
sn5.forEach(function(sn6) {
_.child('user/' + sn6.key + '/lineage/child_link/').on('value', function(sn7) {
sn7.forEach(function(sn8) {
_.child('user/' + sn8.key + '/lineage/child_link/').on('value', function(sn9) {
sn9.forEach(function(sn11) {
_.child('user/' + sn11.key + '/lineage/child_link/').on('value', function(sn12) {
sn12.forEach(function(sn13) {
children.push(sn13.key)
})
});
children.push(sn11.key)
})
});
children.push(sn8.key)
})
});
children.push(sn6.key)
})
});
children.push(sn4.key)
})
});
children.push(sn2.key)
})
});
return children;
显然,问题是这只查询了6层。有什么想法吗?甚至可能?多亏了@georgeawq,这就是我们的解决方案:
children: function(callback) {
function getChildren(a) {
_.child("user/" + a + "/lineage/child_link/").once("value", function(a) {
null != a.val() ? a.forEach(function(a) {
children.push(a.key), getChildren(a.key)
}) : children.sort(); callback(children)
})
}
var children = [];
getChildren($localStorage.currentUser.uid);
},
基本但完美的解决方案。谢谢大家! 感谢@georgeawq,这是我们的解决方案:
children: function(callback) {
function getChildren(a) {
_.child("user/" + a + "/lineage/child_link/").once("value", function(a) {
null != a.val() ? a.forEach(function(a) {
children.push(a.key), getChildren(a.key)
}) : children.sort(); callback(children)
})
}
var children = [];
getChildren($localStorage.currentUser.uid);
},
基本但完美的解决方案。谢谢大家! 这是否可能是一个尝试和观察它是否有效的问题。该准则是否合理/可行,以及。。。你似乎已经知道了,否则你就不会在这里了。那么你真正的问题是什么?这是如何递归地实现的吗?你关心演出吗?如果是,你已经试过了吗?结果与您期望的相比是什么?AngularJS程序员通过以下方式避免了(AKA)错误。将基于异步回调的操作转换为承诺并链接它们。这正是我所需要的。不知道为什么我没想到!这是否可能是一个尝试和观察它是否有效的问题。该准则是否合理/可行,以及。。。你似乎已经知道了,否则你就不会在这里了。那么你真正的问题是什么?这是如何递归地实现的吗?你关心演出吗?如果是,你已经试过了吗?结果与您期望的相比是什么?AngularJS程序员通过以下方式避免了(AKA)错误。将基于异步回调的操作转换为承诺并链接它们。这正是我所需要的。不知道为什么我没想到!