I'm having a bit of trouble trying to manage scope when using getJSON.
So on a HTML page I have an unordered list to be filled with list items from a JSON file. The list is markup for a carousel.
HTML:
<ul class="carousel"></ul>
JS:
grabJSON : function() {
$.getJSON('file.json', function(data) {
var items = [];
$.each(data.foo, function(k, v) {
items.push('<li>' + v.bar + '</li>');
}
$('ul.carousel').append(items.join(''));
// carousel action here
$('ul.carousel').carouselFunction({
// carousel options here
});
});
}
Currently, I have to place the carousel function within the getJSON function. If I make another function for setting up the carousel, I lose the scope within getJSON.
What is a preferred method of breaking this out so I can have a setupCarousel : function() that is called from getJSON? Normally if I want to call a function from another function in an object I can just go this.setupCarousel(), however with nested scope I'm not sure how to handle this.
Also, outside of the getJSON function I do not have access to any of the elements that were appended. So I can access ul, but not any of the list items created while adding them from getJSON.