【问题标题】:How to load JSON.js library with Require JS in order to make a Backbone app work in IE7?如何使用 Require JS 加载 JSON.js 库以使 Backbone 应用程序在 IE7 中工作?
【发布时间】:2014-01-16 13:51:14
【问题描述】:

我已经设法让我的 Backbone 应用程序与 IE8 - IE Edge 一起工作......是的:)

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

'JSON' is undefined - file: backbone.js

还有一部分主干库代码被 IE 的控制台高亮显示:

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

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

好的,谷歌搜索并转到以下库 - 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();
});

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    首先将 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'
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2014-12-18
      • 2016-02-23
      • 1970-01-01
      相关资源
      最近更新 更多