如何定义/使用多个路由使用骨干,requirejs [英] How to define/use several routings using backbone and requirejs

查看:127
本文介绍了如何定义/使用多个路由使用骨干,requirejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分我的应用程序在多个应用程序。

  main.js
app.js
APP1 /
  | - 路由
  | - 控制器
  | - 应用程序
APP2 /
  | - 路由
  | - 控制器
  | - 应用程序

1)当我尝试使用 APP1 路由器,他们的工作。结果
2)当我尝试使用 APP2 路由器,他们不工作。结果
3)如果我评论该行'JS / APP1 /路由中, main.js 路由器 APP2 工作

为什么会这种行为?结果
有没有用在GitHub上多个路由和requirejs应用的一些例子吗?

感谢。

下面是我的code:


** ** main.js

 定义([
    JS /应用程序',
    JS / APP1 /路由',//在此应用程序工作的路由器
    JS / APP2 /路由//在此应用程序的路由器不工作,但
                       //如果我评论了previous线(JS / APP1 /路由,)
                       //它们的工作原理
]
功能(应用程序)
{
    使用严格的;
    App.initialize();
});


** ** app.js

 定义([],
功能()
{
    使用严格的;
    VAR应用=新Backbone.Marionette.Application();    返回程序;
});


** APP1 / rotuing **

 定义(['骨干','APP1 /控制器'],功能(骨干,控制器)
{
    使用严格的;
    VAR路由器= Backbone.Marionette.AppRouter.extend({        appRoutes:{
            *默认值:指数1
        }    });
    回到新的路由器({
        控制器:控制器
    });});


** APP2 / routing.js **

 定义(['骨干','APP2 /控制器'],功能(骨干,控制器)
{
    使用严格的;
    VAR路由器= Backbone.Marionette.AppRouter.extend({        appRoutes:{
            APP2':'索引2'
        }    });
    回到新的路由器({
        控制器:控制器
    });});


解决方案

该问题可能是由在路由器文件加载顺序引起的,而路由器创建的。

骨干历史对象是负责执行的路线。它收集所有路由器上定义的所有路由,当路由器实例化。然后,它监视浏览器的更改URL。当它看到一个变化,它会第一时间提供匹配的路由,并触发一个路线,跳过别的。

当你有一个 *默认路线定义,一切都符合这一点。因此,如果这条线路先加载,没有别的会打。所以,你需要在你的路由参数更明确,使这一路线不打所有的时间,或者你需要确保该路由器为最后的加载。

I divided my app in several apps.

main.js
app.js
app1/
  |- routing
  |- controller
  |- app
app2/
  |- routing
  |- controller
  |- app

1) When I try to use the routers in app1, they work.
2) When I try to use the routers in app2, they don't work.
3) If I comment the line 'js/app1/routing', in main.js the routers in app2 work.

Why do I get this behaviour?
Is there some example of app using multiple routing and requirejs on github?

thanks.

Here's my code:


** main.js **

define([
    'js/app',
    'js/app1/routing', // the routers in this app work
    'js/app2/routing'  // the routers in this app do not work but 
                       // if I comment the previous line (js/app1/routing',) 
                       // they works
],
function (App)
{
    "use strict";
    App.initialize();
});


** app.js **

define([],
function ()
{
    "use strict";
    var app = new Backbone.Marionette.Application();

    return app;
});


** app1/rotuing **

define(['backbone','app1/controller'], function(Backbone, controller)
{
    "use strict";
    var Router = Backbone.Marionette.AppRouter.extend({

        appRoutes: {
            '*defaults': 'index1'
        }

    });
    return new Router({
        controller: controller
    });

});


** app2/routing.js **

define(['backbone','app2/controller'], function(Backbone, controller)
{
    "use strict";
    var Router = Backbone.Marionette.AppRouter.extend({

        appRoutes: {
            'app2': 'index2'
        }

    });
    return new Router({
        controller: controller
    });

});

解决方案

The problem is likely caused by the order in which the router files are loaded, and the routers are created.

Backbone's history object is responsible for executing routes. It collects all routes defined on all routers, when the routers are instantiated. Then it monitors the browser's url for changes. When it sees a change, it will take the first available matching route and fire that one route, skipping anything else.

When you have a *defaults route defined, everything matches this. Therefore, if this route is loaded first, nothing else will hit. So, you would need to be more explicit in your route parameters so that this one route doesn't hit all the time, or you would need to ensure that this router is loaded last.

这篇关于如何定义/使用多个路由使用骨干,requirejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆