Comprender la devolución de llamada "archivo agregado"

En el siguiente ejemplo: https://github.com/blueimp/jQuery-File-Upload/blob/master/basic-plus.html We can find the following callback:

}).on('fileuploadadd', function (e, data) {
    data.context = $('<div/>').appendTo('#files');
    $.each(data.files, function (index, file) {
        var node = $('<p/>')
                .append($('<span/>').text(file.name));
        if (!index) {
            node
                .append('<br>')
                .append(uploadButton.clone(true).data(data));
        }
        node.appendTo(data.context);
    });

I tried to replicate that code, but can't understand why is there an "each" inside that function, as the callback will be executed once per each uploaded file (so if I'm uploading 3 files at the same time, the callback will be executed 3 times). So, why is there a need for an "each" loop here? It seems to me that it'll always loop through one element: the uploaded file.

Please help, I'm desperate :(

preguntado el 23 de septiembre de 13 a las 03:09

Is there a problem? Have you tried it with or without the $.each to see what it does? I'm not exactly sure what the desperation is about. -

@ChrisLively the problem is I don't understand how it works; as it works both with and without the EACH loop, so I'm clearly missing something. -

There are two ways to figure out what's going on in a bit of javascript code. Either use the tools built in the browser to put breakpoints on it and step through it OR add alert() messages within the functions at various points that emits some variable values. Either would show you exactly what is happening. -

1 Respuestas

I think Each loop inside that function is used to append buttons and each file related content Example filename etc.... if you notice the uploadButton has some appended buttonds and click events.

Respondido el 23 de Septiembre de 13 a las 16:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.