0

I have a simple xml file that I'm looping over to set a ValueObject for each item node. Each VO created is added to a LinkedList data structure. The linkedList is instantiated outside the .each() loop. However, for some reason, my linkedList instance returns "undefined" inside the loop. For instance:

Model.prototype.setData = function(data) {  
    this.data = data;

    this.linkedList = new LinkedList();

    this.startItem = $(data).find('slideShow').attr('startItem');
    this.currentID = this.startItem || 1;

    $(data).find('item').each(function() {
        var imageVO = new ImageVO();
        imageVO.id = $(this).find('id').text();
        imageVO.image = $(this).find('image').text();
        imageVO.title = $(this).find('title').text();
        console.log(this.linkedList) //undefined
        this.linkedList.addNode(imageVO);
    });

    this.linkedList.currentNode = this.linkedList.findNodeByIndex(this.currentID);
}

1 Answer 1

1

This is because this.linkedList is revering to the linkedList property of the actual item of the data collection.

Try this:

var that = this;
$(data).find('item').each(function() {
    var imageVO = new ImageVO();
    imageVO.id = $(this).find('id').text();
    imageVO.image = $(this).find('image').text();
    imageVO.title = $(this).find('title').text();
    console.log(that.linkedList) //undefined
    that.linkedList.addNode(imageVO);
});
Sign up to request clarification or add additional context in comments.

2 Comments

I think it's also worth mentioning that the console.log() statement is for an attribute called linkList, not linkedList.
Thanks just noticed that too. I fixed it above to account for "linkedList" not "linkList" (typo). Thanks for the quick reply! This answer just put a lot (about what I know in terms of javascript) into perspective.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.