jQuery的getScript加入 - 包括文件转换成主要适用范围? [英] jQuery's getScript - including files into the main scope?

查看:122
本文介绍了jQuery的getScript加入 - 包括文件转换成主要适用范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也想了很多,我应该怎样包括我Backbone.js的应用程序文件。在生产中,我显然会加入我的文件,并尽量减少他们继续要求在最低限度,但发展过程中,这将是很好,只是有加载的所有文件,不必要求每一个小变化buildscript。

所以,我已经采取了看看的jQuery getScript加入() -method。我尝试过了,并能加载我的文件。

由于我已经把 getScript加入 -call成一个功能,确保之前,我开始我的Backbone.js的应用程序文件被加载,似乎每个脚本加载,不包括到全球范围。

  VAR装载机=功能(){
     变种脚本= ['/app/routers/myrouter.js'];     对于(VAR I = 0; I< scripts.length;我++){
         $ .getScript(脚本[I],函数(){});
     }     的console.log(myrouter); //工程 - myrouter是一个函数
     在里面(); //回调 - 我们已经加载的所有脚本
 };VAR的init =功能(){
    的console.log(myrouter); // myrouter是不确定的
};$(文件)。就绪(装载机());


解决方案

那么首先,你只是叫装载机()强似功能:

  $(文件)。就绪(装载机);

将解决这个问题。

其次,您呼叫的的init 回调的时候了,而不是当加载的所有脚本。如果有很多的脚本,你需要使用的承诺:

  VAR脚本= ['/app/routers/myrouter.js'];
    对于(VAR I = 0; I< scripts.length;我++){
        脚本由[i] = $ .getScript(脚本[I]);
    }
    $ .when.apply($,脚本)。然后(INIT,函数(){
        一些脚本失败;
    });

以上使用的语法是 $时(promise1,promise2,...),那么(successCallback,failureCallback) - 。我们填补阵列的承诺和使用。适用将它们应用作为参数。<​​/ p>

http://api.jquery.com/jQuery.when/

http://api.jquery.com/Deferred.then/

I have thought a lot about how I should include files in my backbone.js-application. In production, I am obviously going to join my files and minimize them to keep requests at a minimum, but during development, it would be nice to just have all files loaded and not having to call a buildscript for every little change.

So I have taken a look at jQuery's getScript()-method. I tried it out and were able to load my files.

As I've put the getScript-call into a function, to ensure that files are loaded before I initiate my backbone.js application, it appears that every script loaded, are not included into the global scope.

var loader = function () {
     var scripts = ['/app/routers/myrouter.js'];

     for (var i = 0; i < scripts.length; i++) {
         $.getScript(scripts[i], function () {});
     }

     console.log(myrouter); // Works - myrouter is a function
     init(); // Callback - we've loaded all scripts
 };

var init = function () {
    console.log(myrouter); // myrouter is undefined
};

$(document).ready(loader());

解决方案

Well first of all, you are just calling loader() instead of passing the function:

$(document).ready(loader);

Will fix that

Secondly, you are calling the init callback right away instead of when all the scripts are loaded. If there are many scripts you need to use promises:

    var scripts = ['/app/routers/myrouter.js'];
    for (var i = 0; i < scripts.length; i++) {
        scripts[i] = $.getScript(scripts[i]);
    }


    $.when.apply( $, scripts ).then( init, function(){
        "some script failed";
    });

The syntax used above is $.when( promise1, promise2, ...).then( successCallback, failureCallback) - we fill the array with promises and use .apply to apply them as arguments.

http://api.jquery.com/jQuery.when/

http://api.jquery.com/Deferred.then/

这篇关于jQuery的getScript加入 - 包括文件转换成主要适用范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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