开发者

yepnope's complete callback question

开发者 https://www.devze.com 2023-04-03 05:58 出处:网络
I am using a \"2-step view\", where I have a layout template (common for all pages) with yepnope\'s load (in the html head) of jQuery and some plugins. Something like:

I am using a "2-step view", where I have a layout template (common for all pages) with yepnope's load (in the html head) of jQuery and some plugins. Something like:

yepnope(['/path/to/jquery.js', '/path/to/jquery.plugin-common.js']);

Sometimes I need another plugin, so within the inner template I do additional (in the html body):

yepnope('/path/to/jquery.plugin-additional.js');

Now that I need to do the actual js magic, can I safely do just:

yepnope({
    complete: function(){...}
});

So, the questions are in fact two:

  1. Is the complete callback fired upon the load completion of the global resources stack? So it's safe to "register" this complete callback anywhere assuming that all needed resources have been registered before?

  2. Can I safely call yepnope just with the "complete" callback option? I mean, as long as I'm not "testing" anything and my resources have been registered already...

I have tried it and it worked, but I'm not fully aware if it's internals, so I just want to make sure that I'm not doing something wrong... Thanks in advance.

--

And one last thing. The manual under preload! says:

yepnope({
    load: 'preload!jquery.1.5.0.js',
    callback: function (url, result, key) {
        window.jQuery; // undefined (but it's cached!);
    }
});

Can you please explain what is this about? I am completely missing the point here..开发者_C百科.


I can help on the preload! question. The idea of preload! is that yepnope will download the file but will not execute it. It transfers the jQuery file, but it will still be undefined after callback is being called, as it was not injected as a script into the page.


In my opinion, you are doing this wrong. I'm surprised that it work, but maybe your scripts are loaded before the call of the "complete" function. I think you should do that:

yepnope({
    load: ['/path/to/jquery.js', '/path/to/jquery.plugin-common.js']
    callback: {
        "jquery.js": function () {
            console.log("jquery loaded!");
        },
        "jquery.plugin-common.js": function () {
            console.log("plugin loaded!");
        }
    }
});

And for the additional plugin in the html body:

yepnope({
    load: '/path/to/jquery.plugin-additional.js'
    callback: function () {
        $(document).ready(function(){
            console.log("plugin-additional loaded!");
        });
    }
});

Of course, replace console.log() by your code related to each plugin that you can safely execute in this context.

for the last question, i can't say anything, because i didn't succeed to make preload! working, maybe it's buggy, maybe i didn't understand how it work...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号