I have a JS code that has a list of elements (name "topicos"). This topicos list has a property named "noticias" wich may have to be published in parallel and afeter the page is loaded. The thing is that when trying to use jquery each() to iterate over the "topicos" list and using the jquery ajax() command to get the respective "noticia" for the current topico, the response is assigned to the wrong "topico". It appears to be a object reference issue; I mean, the variable topico has a reference to one object when the $.ajax is called but when the success callback is invoked the variable topic is referencing another object.
The code:
//topicosVM - list with topics
$(topicosVM).each(function(idx, topico) {
//recuperação das noticias vinculadas
[login to view URL] = [login to view URL]([]);
//Populate the noticias list with the request result =>>> THAT's WHERE THE PROBLEM IS!
(function(tpc){
$.get(apiURL + "/topicos/" + [login to view URL] + "/noticias/", function(data) {
if (data!=null && [login to view URL] > 0) {
var auxNoticias = new Array();
$(data).each(function(){
[login to view URL](this);
});
[login to view URL](auxNoticias);
}
})
})(topico);
//montagem do VM das fases
[login to view URL] = false;
[login to view URL] = false;
[login to view URL] = false;
[login to view URL] = false;
$([login to view URL]).each(function(idx, fase) {
if ([login to view URL] == 1) {
[login to view URL] = true;
}else if ([login to view URL] == 2) {
[login to view URL] = true;
}else if ([login to view URL] == 3) {
[login to view URL] = true;
}else if ([login to view URL] == 4) {
[login to view URL] = true;
}
});
//montagem do VM das perguntas
$([login to view URL]).each(function(idx, pergunta) {
[login to view URL] = [login to view URL](false);
[login to view URL] = [login to view URL](false);
[login to view URL] = [login to view URL]('');
[login to view URL] = [login to view URL](false);
[login to view URL] = [login to view URL](function() {
return "Olá, eu gostaria de saber mais informações sobre \"" + pergunta.title+"\"";
});
[login to view URL] = function() {
var _pergunta = this;
[login to view URL](true);
}
[login to view URL] = function() {
var _pergunta = this;
[login to view URL](function(){
[login to view URL](false);
[login to view URL](![login to view URL]());
});
}
[login to view URL] = function (cb) {
var _pergunta = this;
if ([login to view URL]() == '') {
[login to view URL](true);
$.get(apiURL + "/perguntas/" + [login to view URL], function(data) {
[login to view URL]([login to view URL]);
cb();
});
}else{
cb();
}
}
});
});
[login to view URL] = [login to view URL](topicosVM);;
Hi,
I have more than 4 years of experience in developing rich internet applications and worked a lot with HTML, JS/JQuery and CSS. I think I can definitely help you with this issue.
Thanks,
Shiva
$25 USD in 1 day
3.6 (1 review)
0.8
0.8
4 freelancers are bidding on average $31 USD for this job
Hi,
I've reviewed your project description and the code you provided and I've located the problem, its a somewhat common error so don't feel bad - but it's easy to correct so you can move forward. In addition to correcting the problem, I'll explain the cause so you don't run into the same issue again in the future.
I should be able to have you the corrected script within an hour or two of receiving the script to work on.
Regards,
John