3

I am attempting to load a number of small .html files using jQuery, and have them all put into the DOM before I execute a certain part of code. But this is proving extremely difficult. I can make it do ONE file well enough, but anything I try to make it handle multiples just doesn't function and I cannot comprehend why.

This is the code that can do ONE file.

var templates = (function ($, host) {
    // begin to load external templates from a given path and inject them into the DOM
    return {
        // use jQuery $.get to load the templates asynchronously
        load: function (url, target, event) {
            var loader = $.get(url)
                .success(function (data) {
                    // on success, add the template to the targeted DOM element
                    $(target).append(data);
                })
                .error(function (data) {

                })
                .complete(function () {
                    $(host).trigger(event, [url]);
                });
        }
    };
})(jQuery, document);

This is used as follows;

templates.load("file1.html",
    "#templates-container", "file1-loaded");


$(document).on("file1-loaded", function (e) {
    // execute the rest of the page
});

This falls flat if I need to load more than one file though. So I tried this ...

(function ($, host) {
    $.fn.templates = function (options) {
        var settings = $.extend({
            queue: [],
            element: this,
            onLoaded: function () { }
        }, options);

        this.add = function (url) {
            settings.queue.push(url);
            return settings.element;
        }

        this.load = function () {
            $.each(settings.queue, function (index, value) {
                $.get(value).success(function (data) {
                    $(settings.element).append(data);
                });
            });

            settings.onLoaded.call();
        }

        return settings.element;
    }
})(jQuery, document);

Which is intended to work like this ...

$('#templates-container').templates({
    onLoaded: function () {
        // execute the rest of the code
    }
}).add('file1.html').add('file2.html').done();

But it just outright fails, and it gives me no indication as to why. I don't even get an error message. But the onLoaded never gets called properly.

7
  • first version works fine for me...here's a working plunker demo Commented Oct 28, 2013 at 23:04
  • That isn't the way I am needing to use it. You're doing two separate loads. I need to do both loads in ONE call, so that the event only raises when they are ALL complete. Commented Oct 28, 2013 at 23:07
  • And it needs to take an infinite number of files, not just one. Commented Oct 28, 2013 at 23:07
  • OK that wasn't clear...you just stated that it all failed. Description could use some upgrading. Can build a deferred array and resolve it when all are done. Give me a few Commented Oct 28, 2013 at 23:09
  • Oh, I am very sorry. I thought I had said that. I will get to updating the text. My apologies. Commented Oct 28, 2013 at 23:25

1 Answer 1

1

Here's a solution that sends an array to loader function , pushes the promise for each request into an array, then when the whole promise array is resolved using $.when your event fires

var templates = (function ($, host) {
    // begin to load external templates from a given path and inject them into the DOM
    return {
        // use jQuery $.get to load the templates asynchronously
        load: function (templateArray, target, event) {
            var defferArray = [];
            $.each(templateArray, function (idx, url) {
                var loader = $.get(url)
                    .success(function (data) {
                    // on success, add the template to the targeted DOM element
                    $(target).append(data);
                })
                defferArray.push(loader);
            })

            $.when.apply(null, defferArray).done(function () {
                $(host).trigger(event);
            });
        }
    };
})(jQuery, document);

$(document).on("files-loaded", function () {
    $("#content").append("<p>All done!</p>");
})


$(function () {
    var templateArray = ["file1.html", "file2.html"]
    templates.load(templateArray, "#content", "files-loaded");
});

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

this is same as other link in comments...just added as answer for others

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.