变量在作用域中不存在(使用requirejs/backbone)

variable doesn't exist in scope (using requirejs/backbone)

本文关键字:requirejs backbone 使用 作用域 不存在 变量      更新时间:2023-09-26

以下是我的bb视图的一部分:

define(['backbone', 'tools/logger', 'icanhaz', 'view/loader', 'text!template/root.ich',
    'view/users', 'view/chart/monthly_balance', 'datatables'],
function(Backbone, logger, ich, loader, template,
    UsersView, MonthlyBalanceChartView) {
    // here I can see both `loader` and `template` variables
    'use strict';
    return Backbone.View.extend({
        [...]
        initialize: function(options) {
            logger.init('root');
            // I can't see `loader` and `template` here
        },
        [...]
    });
});

问题是,我不能看到视图的初始化方法中的两个变量,我不知道为什么。我觉得应该把它封起来。我使用谷歌浏览器的开发人员工具断点进行测试。在use strict之前,我可以看到一切(加载器和模板变量设置)。所有其他变量都可以访问。

例如,整个加载器文件是:

define(
    ['icanhaz', 'tools/logger'],
    function(ich, logger) {
        return {
            loadTemplate: function(tpl) {
                $(tpl).each(function() {
                    if (this.outerHTML) {
                        ich.addTemplate(this.id, $(this).text());
                        logger.log('LOADED', this.id);
                    }
                });
            }
        };
    });

这些变量未定义的原因是什么?

打开控制台,在network选项卡中,尝试查看是否加载了这个js文件

也许你需要使用shim命令来加载非模块文件

阅读这里的shim例子。https://github.com/requirejs/example-jquery-shim

希望能有所帮助。