我正在触发child_added
事件,该事件会在将新子节点添加到节点(父节点)时进行侦听。其中,是另一个事件value
。要从firebase加载数据,需要几毫秒,在此时,在检索数据之前执行下一行。我已经使用了then()的承诺甚至尝试过回调,但两者都不适合我。我的代码有什么问题吗?我想要的是,当数据完全加载时,应该调用pupoluateUsers()
函数。其他智者等到它。
The promise method I have tried
The callback method I have tried
示例代码,我现在正在使用:
$("body").on("click",".si-add",function(e){
var _CU = $.cookie("_cu"),
_MT = $(this).parent().attr("data-id"),
_chattedWith = [],
_child_key = [];
_firebaseChatRef.child(_CU).child(_MT).set(true).then(function(){});
_firebaseChatRef.child(_CU).on("child_added",function(res){
_firebaseAuthRef.child(res.key).on("value",function(r){
populateUsers({name:r.val().n,id:r.key});
})
})
function populateUsers(chattedWith){
let len = chattedWith.length,
_el = document.getElementsByClassName("chat-ind-body-wrap")[0];
_el.insertAdjacentHTML('afterbegin','<li class="chat-ind-user active-user" data-id="'+chattedWith.id+'">'
+'<div class="chat-ind-user-img">'
+'<img src="assets/1.jpg">'
+'</div>'
+'<div class="chat-ind-user-data">'
+'<div class="ciu-name"> '+chattedWith.name+' </div>'
+'<div class="ciu-text"> Hi, I am the text here ! </div>'
+'<span class="ciu-time"> 2h ago </span>'
+'</div>'
+'<span class="user-availble">'
+'<i class="fa fa-circle"></i>'
+'</span>'
+'</li>');
}
});
&#13;