0

Would anyone mind explaining to me why...

$(document).ready(function() {
    var scu = ['0291285', '0409338', '0521704', '0521990', '0523652', '0523657', '0523660', '0523704'];
    var inData = $('#output');
    var testdiv = $('#testdiv');
    function Item(scu, description, price, extended, type) {
        this.scu = scu;
        this.description = description;
        this.price = price;
        this.extended = extended;
        this.type = type;
        //this.saved = function() {};
    }
    var rows = [];
    function get() {
        inData.html('');    
        $.each(scu, function(index, val) {
            $.post('chBuild.php', {scu:val}, function(output) {
                $.each(output, function(i, obj) { 
                    var i = 0;
                    rows[i] = new Item(obj.scu, obj.description, obj.price, obj.extended, obj.type);
                    console.log(rows[i].price)
                                    //this logs every object but...                 

                    i =+ 1;
                });
            }, 'json');         
        });
        console.log(rows[0].price);

            //this says rows[0] is undefined?

    }
    inData.click(get);
});

I am trying to find the best way to create and store multiple objects.

2
  • 1
    You shouldn't do var i = 0 inside the each, this way every item will be assigned to position 0. The last statement (i += 1;) is irrelevant, since that variable only exists inside that closure (i.e. every loop of the iteration would redefine i). Use the i provided as argument (function(i,obj) {), it will be accurate in your case. Commented Jun 3, 2012 at 2:11
  • @mgibsonbr is on the money, the extra var i is always going to be zero. Also remove the i= +1 Commented Jun 3, 2012 at 3:00

2 Answers 2

3
$.post('chBuild.php', {scu:val}, function(output) {
            $.each(output, function(i, obj) { 
                var i = 0;
                rows[i] = new Item(obj.scu, obj.description, obj.price, obj.extended, obj.type);
                console.log(rows[i].price)
                i =+ 1;
            });
        }, 'json');         

Here the call to $.post is asynchronous, is going to be filled just when the ajax call returns. Maybe you should make it synchronous

$.ajax({'url': 'chBuild.php', 'async': false, ...);
Sign up to request clarification or add additional context in comments.

Comments

3

It's because $.post is asynchronous. The each only start the HTTP request, but it returns immediatly, so when the second console.log runs the item had not been created yet.

2 Comments

Is there a way to make it run only after $.post?
You mean, after all posts for all itens have run? You could make the requests synchronous as JoseP suggested. There might be other ways, but I can't answer that from the top of my head.

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.