2

相互参照している 2 つのモデルがあります。これは次のようになります。

メインモデル:

define(
    [ 'durandal/app', 'durandal/plugins/router', 'models/Shell', 'models/EditModel' ],
    function (app, router, shell, editModel) {
        //...
        return {
            //...

            // This function should be accessible by the EditModel 
            update: function() {
                //...
            },

            showEditView: function() {
                // Initialise the EditModel with some data and show the according view afterwards
                editModel.init('set some important stuff here...');
                router.navigateTo('#/EditView');
            }
            //...
        };
    }
);

モデルの編集:

define(
    [ 'durandal/app', 'durandal/plugins/router', 'models/Shell', 'models/MainModel' ],
    function (app, router, shell, mainModel) {
        //...
        return {
            //...

            // This function should be accessible by the MainModel 
            init: function() {
                //...
            },

            showMainView: function() {
                // Update the the MainModel with some data and show the according view afterwards
                mainModel.update('set new data here...');
                router.navigateTo('#/MainView');
            }
            //...
        };
    }
);

残念ながら、これは機能していません。MainView でページを読み込んで showEditView を呼び出すと、変数 editView が認識され、すべて正常に動作しますが、EditModel の変数 mainModel が定義されていないため、呼び出しmainModel.update(...)が失敗します。EditView に自分のページを「反対方向」にロードすると、同じことが起こります (EditModel の var mainModel はわかっていますが、MainModel の editModel は定義されていません)。

これは既知の問題ですか? その場合: どうすれば回避できますか?

この質問はデュランダルの Google グループにも投稿しました

ありがとう

4

1 に答える 1

2

循環依存関係については、requierejs のドキュメントを確認してくださいhttp://requirejs.org/docs/api.html#circular

循環依存はまれであり、通常は設計を再考する必要がある兆候です。ただし、必要になる場合もあります。その場合は、上記のように require() を使用してください。

main.js の場合、require を依存関係として追加してから、明示的に requireを実行する必要models/EditModelがあります。それを他のモジュールに複製するか、rethink the design;-)。

define(
    [ 'require', 'durandal/app', 'durandal/plugins/router', 'models/Shell', 'models/EditModel' ],
    function (require, app, router, shell, editModel) {
        //...
        return {
            //...

            // This function should be accessible by the EditModel 
            update: function() {
                //...
            },

            showEditView: function() {
                // Initialise the EditModel with some data and show the according view afterwards
                require('models/EditModel').init('set some important stuff here...');
                router.navigateTo('#/EditView');
            }
            //...
        };
    }
);
于 2013-07-19T10:16:14.027 に答える