In relation to this question, I'm trying to add a callback to get the data back. So I tried this:
var subgroupIds = [];
var that = this;
this.getSubGroups = function (groupId,callback) {
var anotherObject = this;
this.getGroups("groupId="+groupId, function(groups) {
if ($.isEmptyObject(groups)) {
return;
} else {
$.each(groups, function(index,group) {
subgroupIds.push(group.id);
that.getSubGroups(group.id);
});
anotherObject.callback(group.id);
}
});
}
I thought I have a better understanding of closure after the previous question but I guess I don't...I'm getting the following error:
Uncaught TypeError: Object [object Window] has no method 'callback'
What am I doing wrong here?
Edit
Here's the content of getGroups:
this.getGroups = function(filter,callback,error_callback) {
this.getJSON('/'+apiVersion+'/groups/',function(data){
// run through the filter engine
output = runFilter(data, filter);
callback(output);
},error_callback);
}
this.getGroupsshould beanotherObject.getGroups?anotherObjectinstead ofthat? And why do you callback to a property, don't you want to callback to the function argument?this/that? When you are declaringthis.getSubGroups,thisrefers towindow, so you're not doing something right to getthisto refer to the right thing. Then,anotherObjectjust refers to the same,window. Also, why are you trying to useanotherObject.callback(group.id)? Don't you just wantcallback(group.id);?