javascript - 第一次加载主干 View 时出现问题

标签 javascript jquery html backbone.js requirejs

我想在加载 html 页面时自动加载我的 JavaScript 之一。 我要加载我的 js 文件的 html 页面。

<!DOCTYPE html>
<html>

<head>
    <script src="/static/js/LoginApp.js"></script>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
</head>

<body>
    <div class="container">
        <h1>Backbone Tutorial Blogroll App</h1>
        <button type="button" id="newButton">Click Me!</button>
        <script src="/static/js/require.js"></script>
        <script src="/static/js/LoadView.js"></script>
    </div>
</body>

</html>

我要加载的 JavaScript 是

LoadView.js

define([
        'jquery',
        'underscore',
        'backbone',
        'LoadView',
        'text!NewViewCheck.html',

        ], function($, _, Backbone, LoadView, NewViewCheck) {
    var Task = Backbone.Model.extend({
        defaults: {}
    });
    TheView = Backbone.View.extend({
        model: new Task(),
        events: {
            'click #newButton': 'initializeView',
        },
        //'template' provides access to instance data when rendering the view
        template: _.template(NewViewCheck),
        initialize: function() {
            console.log('Inside the initialize function');
            this.render();
        },
        render: function() {
            //this.$el.html(this.template());
            console.log(labelsLocale);
            this.$el.html(this.template({}));
            $('#dialogContent').empty().append(this.$el);
            $('#addUserDefinedOption').modal('show');
        },
        initializeView: function() {
            var theView = new TheView();
            console.log('abc');
        },
    });
    $(document).ready(function() {
        //console.log('abc');
    })
});

我收到以下错误 未捕获错误:不匹配的匿名define()模块

请帮助我提前致谢。

最佳答案

该错误是因为您有:

<script src="/static/js/LoadView.js"></script>

docs说:

If you manually code a script tag in HTML to load a script with an anonymous define() call, this error can occur.

这就是你正在做的事情。

在解决方案中:

  • Be sure to load all scripts that call define() via the RequireJS API. Do not manually code script tags in HTML to load scripts that have define() calls in them.

  • If you manually code an HTML script tag, be sure it only includes named modules, and that an anonymous module that will have the same name as one of the modules in that file is not loaded.

因此,只需使用主文件中的 requireJS 加载它或命名模块即可

关于javascript - 第一次加载主干 View 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976989/

相关文章:

javascript - 滚动后更改导航栏颜色但不是立即

javascript - Bootstrap 3 模态上的额外滚动条

php - 使用 HTML 和 PHP 将表单数据发送到电子邮件

javascript - 使用 AJAX 和 JQuery 在没有页面刷新的情况下提交表单时遇到问题

javascript - 哪个是包装历史 API 的最佳 JavaScript 库?

javascript - Bootstrap 工具提示看起来很简单

javascript - 如何在页面加载时启用 Extjs 过滤器

javascript - 如何缩放博客幻灯片中的图像以适合滑动条?

javascript - HTML 中的内联 Javascript 无法加载

javascript - 在添加新标记之前删除以前的标记