Yepnope和"代理" -ing jQuery的准备()函数 [英] Yepnope and "Proxy"-ing jQuery's ready() function

查看:124
本文介绍了Yepnope和"代理" -ing jQuery的准备()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是(非常)慢慢变了大规模的静态文件的网站了解最新的所有最新的东西。我使用yepnope(新包装Modernizr.load)来处理异步加载的依赖,我需要它,在此期间,与目前codeBase的向后兼容。

I'm (very) slowly getting a massive static-file site up to date with all the latest goodies. I am using yepnope (packaged with the new Modernizr.load) to handle async dependency loading and I need it to, in the meantime, be backwardly compatible with the current codebase.

这要求我要么包装的每一个jQuery的调用整个站点(太大的任务,在不久的将来做)的 yepnope()的包装,的更具创造性的解决办法是的代理的jQuery的就绪()通过我自己的功能,并延缓任何执行 $ code,直到从yepnope回调准备好... jQuery的实际就绪()功能将成为处理器一旦yepnope测试满意jQuery的准备。有意义吗?

This requires that i either wrap every single jQuery call sitewide (too big of a task to do in the immediate future) in yepnope()'s wrapper, or the more creative solution is to proxy jQuery's ready() function through my own, and to delay the execution of any $ code until the callback from yepnope is ready... jQuery's actual ready() function will be the handler once the yepnope test is satisfied with jQuery's ready. Make sense?

这个问题在手:

我在这里建立一个灾难?这是
  一个合适的解决方法我
  不善的短期问题
  意大利面条的实施乱七八糟的网站范围
  DOM为中心的code?

Am I creating a disaster here? Is this a suitable workaround for my short-term issue of a poorly implemented sitewide mess of spaghetti DOM-centric code?

更新#3

一个更想好了,出办法,也占了不准备在那里匿名函数刚过直的jQuery。这是在尽量少做,以preserve $只是功能的第三次尝试(函数(){})呼叫以及 $(文件)。就绪()来电。

A more well-thought-out approach that also accounts for non-ready where anonymous function is just passed straight to jQuery. This is a third attempt at doing as little as possible to preserve just the functionality of $(function(){}) calls as well as $(document).ready() calls.

    (function( window, undefined ) {
    var jQuery = (function(fn) {
    if (typeof fn === 'function') {
        yepnope({
            test: window.jQuery.length,
            complete: function() {
                fn();
            }
        });
    }
    var jQuery = 
        _jQuery = window.jQuery,
        _$ = window.$;
    jQuery = jQuery.prototype = {
        constructor: jQuery,
        ready: function( fn ) {
            yepnope({
                test: window.jQuery.length,
                complete: function() {
                    fn();
                }
            });
        }
    };
    return jQuery;
    });
    window.jQuery = window.$ = jQuery;
    })(window);

这是pretty性感,因为虽然它复制的jQuery的装载行为,它不具有的长度,本质上......用yepnope来测试 window.jQuery.length ,它只会返回真正曾经在 jQuery是加载(代理不具有长度)!

This is pretty sexy because while it duplicates the loading behavior of jQuery, it doesn't have a length, inherently... by using yepnope to test for window.jQuery.length, it will only return true once the REAL jQuery is loaded (the proxy doesn't have a length)!

我认真地寻求任何批评 - 尤其是来自亚历塞克斯顿:)哈哈

I earnestly seek any criticism - especially from Alex Sexton :) lol

推荐答案

尽量选用例如philwinkle的,它不工作(至少造成临时的jQuery可变曾在FF3.6。长度支撑)。

Tried to use example of philwinkle, it doesn't work (at least resulting temp jQuery variable had .length prop in FF3.6).

用Google搜索了一会儿,发现 JQL,异步jQuery的装载机,该有办法应付的$(document)。就绪jQuery的实际加载之前调用。借了一些code,得到:

Googled for a while and found jQl, asynchronous jQuery loader, which have a way to cope with $(document).ready called before actual jQuery loading. Borrowed some code and got:

<script type="text/javascript">
var jQ = {
    queue: [],
    ready: function (f) {
        if (typeof f=='function') {
            jQ.queue.push(f);
        }
        return jQ;
    },
    unq: function () {
        for (var i = 0; i < jQ.queue.length; i++) jQ.queue[i]();
        jQ.queue = null;
    }
};
if (typeof window.jQuery == 'undefined') { window.jQuery = window.$ = jQ.ready; }

yepnope({
    load: '//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js',
    complete: function () {
        jQ.unq();
    }
});
</script>

这篇关于Yepnope和&QUOT;代理&QUOT; -ing jQuery的准备()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆