4

I'm having a problem with a circular reference in my marionette.js application..

The problem is:

App.js creates the router with an controller, and that controller requires the app.js again so it can add the views to the regions.. As you can see below (the controller) when i print the Application it returns undefined because of the circulare reference..

controller.js:

define(
['app', 'views/ProjectItemView'],
function (Application, ProjectItemView)
{
    'use strict';
            console.log(Application); // undefined

    return Marionette.Controller.extend({
        showProjects : function()
        {
            Application.main.show(new ProjectItemView());
            console.log('project');
        }
    });
}
);

router.js:

   define
    (
        ['marionette'],
        function(Marionette)
        {
            'use strict';

            return Marionette.AppRouter.extend
            (
                {
                    appRoutes:
                    {
                        'dashboard/projects' : 'showProjects'
                    }
                }
            );
        }
    );

app.js

define
(
    [
        'backbone', 'marionette', 'jquery', 'routers/DashboardRouter', 'controllers/DashboardController',
        'views/dashboard/Header'
    ],
    function(Backbone, Marionette, $, DashboardRouter, DashboardController, Header)
    {
        'use strict';

        var app = new Marionette.Application();

        app.addRegions({
            header: '#header',
            main: '#main',
        });

        app.header.show(new Header());

        app.on('initialize:after', function () {
            Backbone.history.start({pushState: true});
            app.router = new DashboardRouter({
                controller : DashboardController
            });
        });

        $(document).on("click", "a[href^='/']", function(event)
        {
            var href = $(event.currentTarget).attr('href');
            console.log(href);

            event.preventDefault();
            app.router.navigate(href, {trigger: true});
            return false;
        });

        return app;
    }
);

Now what is the best way to solve this problem? I'm totally new to this so i don't really know what the best practice is..

Also is it correct that I add the views in my controller? Or should i do that somewhere else?

Thanks in advance

4

3 回答 3

4

我认为您当时应该将一个区域传递给控制器​​以实例化它,您肯定需要移动代码。我有一个示例应用程序正在工作,但不需要使用,但是在控制器中创建一个区域并将一个区域从主应用程序传递给它的概念可以在这里演示。

 App.addInitializer(function () {
    var controller = new BookStoreController({region: App.mainRegion});
    layout = new CatalogLayout();
    controller.region.show(layout);
    var router = new Router({controller:controller});
});

http://jsfiddle.net/rayweb_on/hsrv7/11/

希望有帮助。

于 2013-06-12T17:03:31.667 回答
1

如果我理解正确的话,基本上,你有路由器依赖于应用程序和控制器依赖于应用程序。

如果是这样的话,你只需将它们分成三个模块,App、Router 和 Controller。然后在 Router 和 Controller 模块中定义 ['app',...] 。 是我用于 JMQ 的其他一些基本代码结构。

更新:从Using-marionette-with-requirejs,它还讨论了“避免循环依赖”

于 2013-06-13T03:25:09.337 回答
0

您可以将 ProjectView 放入 Marionette 模块。

有关此类技术的示例,请参阅https://github.com/mallim/backbone_examples/blob/master/app/scripts/cats/module.js 。

希望这可以帮助。

于 2013-06-13T01:58:27.437 回答