代码之家  ›  专栏  ›  技术社区  ›  SamBrick

尝试使用requirejs实例化Marionette应用程序时出现“对象不是函数”错误

  •  2
  • SamBrick  · 技术社区  · 11 年前

    这是我第一次尝试使用Marionette。我正在尝试将Marionette应用程序实例化为requirejs模块。我一直在关注Marionette.js维基上的Using Marionette with Require js文章:-

    https://github.com/marionettejs/backbone.marionette/wiki/Using-marionette-with-requirejs

    我想我已经把所有的垫片、依赖项和文件都整理好了,因为我可以在同一个地方实例化视图、模型等,而不会出错,但我无法解决我的应用程序的问题。如有任何帮助或指导,我们将不胜感激!

    这是我的index.html:-

    <!DOCTYPE html>
    <html>
    <head>
        <title>Marionette Structure and Require AMD Proto</title>
        <link rel="stylesheet" type="text/css" href="css/styles.css">
    </head>
    <body>
    <div id="nav">
    
    </div>
    <div id="mainContent">
    
    </div>
    <script language="javascript">
        // convenience function, because console.log throws errors and crashes IE
        // this way you don't need to all logs to test in IE
        function trace(msg){
            try{
                console.log(msg);
            }catch(e){
                // suppressed error
            }
        }
    </script>
    <script src="js/lib/require.js" data-main="app.js"></script>
    </body>
    </html>
    

    这是我的app.js:-

    require.config({
        paths : {
            backbone : 'js/lib/backbone',
            underscore : 'js/lib/underscore',
            jquery : 'js/lib/jquery',
            marionette : 'js/lib/backbone.marionette'
        },
        shim : {
            jquery : {
                exports : 'jQuery'
            },
            underscore : {
                exports : '_'
            },
            backbone : {
                deps : ['jquery', 'underscore'],
                exports : 'Backbone'
            },
            marionette : {
                deps : ['jquery', 'underscore', 'backbone'],
                exports : 'Marionette'
            }
        }
    })
    
    require(
        ["jquery",
            "underscore",
            "backbone",
            "marionette",
            "js/shell/shellapp"
        ],
        function($, _, Backbone, Marionette, ShellApp) {
            $(function() {
               new ShellApp();
                trace("ShellApp: "+ShellApp);
            });
        }
    );
    

    最后是我的shellapp.js:-

    define( ["marionette"], function (Marionette) {
    
        // set up the app instance
        var ShellApp = new Marionette.Application();
    
        // configuration, setting up regions, etc ...
        ShellApp.addRegions({
            nav: '#nav',
            main: '#mainContent'
        });
        ShellApp.on('initialize:after', function(){
            trace("initialize:after");
        });
        // export the app from this module
        return ShellApp;
    });
    

    把所有这些放在一起,我在app.js的第42行得到了“UncaughtTypeError:object不是函数”

    非常感谢所有走到这一步的人!

    山姆

    1 回复  |  直到 11 年前
        1
  •  3
  •   c24w FDinoff    11 年前

    我的回答太长了,无法发表评论!

    您正在导出一个构造的Object,而不是构造函数本身:

    var ShellApp = new Marionette.Application()
    ...
    return ShellApp;
    

    这正是导入的内容,因此您不需要创建另一个 new

    首先,我会重新命名 ShellApp shellApp 表示实例,而不是构造函数(这是一种常见的命名约定)。我认为,在教程中,他们打破了这一惯例,这是非常误导人的:

    MyApp = new Backbone.Marionette.Application();
    

    (假设我在这里是对的)。

    我现在假设您只需传递 Marionette.Application 在您的应用程序的生命周期内。

    在教程中 shows 导出 新的 Marionette应用程序 (与您所做的相同),但在导入时并没有显示它实际正在使用。导入对象后,可以访问其属性,例如:

    shellApp.addInitializer(function(options){
        // stuff
    });
    

    更多 here .