2

マリオネット初挑戦です。マリオネット アプリケーションを requirejs モジュールとしてインスタンス化しようとしています。私は、Marionette.js wiki の 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 行目に「Uncaught TypeError: object is not a function」が表示されます

ここまで来てくれた人、本当にありがとう!

サム

4

1 に答える 1

3

私の答えはコメントには長すぎました!

コンストラクター自体ではなく、構築されたオブジェクトをエクスポートしています。

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

これはまさにインポートされるものであるため、別のnewものを作成する必要はありません。

まず、コンストラクター (一般的な命名規則) ではなく、インスタンスを示すために名前を変更ShellAppします。shellAppチュートリアルでは、彼らがこの慣習を破っているのはかなり誤解を招くと思います:

MyApp = new Backbone.Marionette.Application();

(ここで正しい行にいると仮定します)。

ここで、アプリの存続期間中、この 1 つのインスタンスを単に渡すだけであると仮定しますMarionette.Application

チュートリアルでは、 newed-upのエクスポートを示していますが (あなたが行ったのと同じです)、インポート時に実際に使用されていることは示していません。オブジェクトをインポートすると、次のようなプロパティにアクセスできます。 Marionette.Application

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

詳細はこちら

于 2013-04-11T13:52:06.023 に答える