Require.js丢失了url上下文 [英] Require.js loosing url context

查看:94
本文介绍了Require.js丢失了url上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此网址:

"localhost/app2/app.html"

我希望require.js操纵路径,以便它加载的所有脚本都以上下文'app2 /'作为前缀。但是,我的应用程序脚本获得了404,当我查看firebug时,浏览器正在尝试在没有app2上下文的情况下检索它。

I would expect require.js to manipulate the paths so that all scripts it loads are prefixed with the context 'app2/'. However, I get a 404 for my app script, and when I look in firebug the browser is trying to retrieve it without the app2 context.

我的文件结构是:

content
  |-app.html
  |-main.js
  |-app
    |-app.js
    |-main.js
  |-libs
    |-require.js

在app.html中我有一个脚本标记:

In app.html I have a single script tag:

<script src="libs/require.js" data-main="app/main" type="text/javascript"></script>

main.js文件包含:

The main.js file has this:

require.config({
    baseUrl: "/",
    paths: {
        xx: "app/app",
        header: "app/views/header",
        footer: "app/views/footer",
        templates: "app/views/templates",
        jquery: "libs/jquery-2.0.3",
        k: "libs/kendo/kendo.core",
        backbone: "libs/backbone",
        marionette: "libs/backbone.marionette",
        wreqr: "libs/backbone.wreqr",
        babysitter: "libs/backbone.babysitter",
        underscore: "libs/underscore",
        text: "libs/text"
    },
    shim: {
        jquery: {
            exports: '$'
        },
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        marionette: {
            deps: ['jquery', 'underscore', 'backbone'],
            exports: 'Marionette'
        }
    }
});

require(["xx"], function (app) {
    app.start();
});

我的app.js看起来像这样:

And my app.js looks like this:

define(['jquery', 'underscore', 'backbone', 'marionette'],
function ($, _, Backbone, Marionette) {
    var app = new Backbone.Marionette.Application();

    app.addRegions({
        headerRegion: "#header-region",
        moduleRegion: "#module-region",
        footerRegion: "#footer-region",
        dialogsRegion: "#dialogs-region"
    });

    app.on("initialize:after", function() {
        console.log("Starting App");
        Backbone.history.start();
    });

    return  app;
});

因此,当我点击浏览器中的网址时,我看到首先加载了app.html文件,然后加载 require.js ,然后加载 main.js 。之后,我得到404试图从 localhost / app / app.js 加载app.js - app2 缺少上下文。

So, when I hit the url in the browser I see the app.html file is loaded first, then require.js is loaded followed by main.js. After that I get a 404 trying to load app.js from localhost/app/app.js -- the app2 context is missing.

我已经尝试重命名文件,以防需要混淆(不起作用),我尝试上下移动xx路径在列表中查看它是否是一个位置错误(它不是)。我尝试使用从 ./ 开始的xx路径,但这也不起作用。我已经使用浏览器导航到app.js,在预期的路径上,我很高兴为我的http服务器提供服务。

I have tried renaming the files, in case require is getting confused (didn't work), and I have tried moving the xx path up and down in the list to see if it's a positional bug (it's not). I have tried using a path for xx starting with ./, and that doesn't work either. I have navigated to the app.js with the browser, on the expected path, and it is happily served by my http server.

起初我认为这是一个路径问题,所以我在stackoverflow上阅读这里的解决方案,但这不是一个简单的路径问题,因为它是缺少的上下文 - 根URL - 而不是路径。 AHGA。

At first I thought that this was a path problem, so I read the solutions here on stackoverflow, but it's not a simple path problem, because it is the context that's missing -- the root url -- not the path. AHGA.

推荐答案

好的,我有答案。我已将baseUrl设置为绝对URL,而不是相对URL。正确的字符串是:

Ok, I have the answer. I had set the baseUrl to an absolute URL, rather than relative. The correct string is:

baseUrl: './'

这篇关于Require.js丢失了url上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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