如何使用Require JS加载JSON.js库以使Backbone应用程序在IE7中运行?

时间:2014-01-16 13:51:15

标签: javascript json internet-explorer backbone.js requirejs

我设法使我的Backbone应用程序与IE8一起工作 - IE Edge ... Yey:)

最后一块石头是IE7 - 我收到以下Backbone错误:

'JSON' is undefined - file: backbone.js

IE控制台突出显示了骨干库代码的一部分:

s.data=JSON.stringify(i.attrs||e.toJSON(i)

根据我的阅读,我必须加载JSON2或JSON3库,因为IE7没有它。

好的,用Google搜索并转到以下库 - JSON 3:

http://cdnjs.cloudflare.com/ajax/libs/json3/3.2.6/json3.min.js

现在,我使用require js加载我的应用程序,我不知道如何将其与我的应用程序配置集成。

它的依赖关系是什么,它会导出什么?

这是我对require js的配置:

/**
 *  Config.
 */

require.config({

    paths: {
        "jquery" : "libs/jquery",
        "underscore" : "libs/underscore",
        "backbone" : "libs/backbone",
        "text" : "libs/require/text",
        "global" : "libs/global",
        templates: '../templates'
    },

    shim: {

        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        },

        underscore: {
            exports: '_'
        },

        text : {
            exports : 'text'
        }
    },

    global : {
        deps: ["jquery"],
        exports : 'Global'
    }
});

require([

    'jquery',

    'underscore',

    'backbone',

    'router',

    'global'

], function ($, _, Backbone, Router) {

    // Compatibility override, add a close function for the Backbone views.

    var router = new Router();

    Backbone.history.start();
});

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

首先将json库添加到路径配置中:

paths: {
    "json": "http://cdnjs.cloudflare.com/ajax/libs/json3/3.2.6/json3.min.js",
    "jquery" : "libs/jquery",
    "underscore" : "libs/underscore",
    "backbone" : "libs/backbone",
    "text" : "libs/require/text",
    "global" : "libs/global",
    templates: '../templates'
},

json库没有依赖项并导出全局变量JSON。您需要使主干库依赖于json库,并使用shim配置加载JSON库。见下文:

shim: {

    json: {
        exports: 'JSON'
    },

    backbone: {
        deps: ["underscore", "jquery", "json"],
        exports: "Backbone"
    },

    underscore: {
        exports: '_'
    },

    text: {
        exports: 'text'
    }
}