Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干.js路由拦截器_Backbone.js_Routing_Routes_Interceptor - Fatal编程技术网

Backbone.js 主干.js路由拦截器

Backbone.js 主干.js路由拦截器,backbone.js,routing,routes,interceptor,Backbone.js,Routing,Routes,Interceptor,我有带子应用程序的主应用程序: 主应用程序 |-mainRouter.js |-sub_应用程序 |-subAppRouter.js subAppRouter.js扩展了mainRouter.js。subAppRouter.js具有路由处理程序(例如/app1/item/)。我没有访问subAppRouter的权限。 以下是我需要的: 在mainRouter中,我想创建一个路由,该路由将处理来自所有应用程序的所有URL。 它应该处理路由,进行一些检查,在一种情况下,它应该继续从该url的子App

我有带子应用程序的主应用程序:
主应用程序
|-mainRouter.js
|-sub_应用程序
|-subAppRouter.js

subAppRouter.js扩展了mainRouter.js。subAppRouter.js具有路由处理程序(例如/app1/item/)。我没有访问subAppRouter的权限。
以下是我需要的:
在mainRouter中,我想创建一个路由,该路由将处理来自所有应用程序的所有URL。 它应该处理路由,进行一些检查,在一种情况下,它应该继续从该url的子AppRouter启动处理程序,否则它应该进行重定向(例如/app2/somepage)

有人能帮我找到最好的解决方法吗?
换句话说:如何在主干网中通过路由器实现拦截器模式


谢谢

我将把你的问题重新表述为几点

1-您有一个用于普通路由的主路由器

2-您有一个专门的路由器用于某些应用程序路由

3-你需要你的主路由器选择天气来处理转发到子路由器的路由

为了实现这一点,我建议如下

1-创建主路由器,扩展主干。路由器

var mainRouter = Backbone.Router.extend({
    routes:{
        'index':'loadIndex',
        //other common app routes......
    },
    loadIndex: function(){
        //code to load index
    }
});
2-然后为app定义子路由器,扩展主路由器,但请注意路由是如何定义的

var subAppRouter = mainRouter.extend({
    initialize: function(){
        // here we will extend the base routes to not lose default routing, and add app special routing
        _.extend(this.routes, {
           'subApp/index': 'subAppIndex'
       });
    },
    subAppIndex: function(){
         // code to load sub app index
    },
});
然后您可以使用包含基本路由的子路由器。

是一篇关于子例程的好文章。这对我来说非常合适

在项目中包括子例程js库:

<script type="text/javascript" src="backbone.subroute.min.js"></script> 

HTML正文示例:

<a href="#app1/">App1</a>
<a href="#app2">App2</a>
<div class="app">
</div>

JS代码示例:

var MyApp = {};
MyApp.App1 = {
    Router: Backbone.SubRoute.extend({
        routes: {
            "": "init",
            "sub1": "sub1"
        },
        init: function  () {
            console.log("app1");
            $(".app").html($("<h1 />", {text: "App1"}));
            $(".app").append($("<a/>", {href: "#app1/sub1", text: "sub1"}));
        },
        sub1: function  () {
            console.log("sub1");
            $(".app").append($("<h2 />", {text: "sub1"}));
        }
    })
};
MyApp.Router = Backbone.Router.extend({
    initialize: function  () {
        if(!MyApp.Routers){
            MyApp.Routers = {};
        }
    },
    routes: {
        "app1/*subroute": "invokeApp1Router",
        "app2": "app2"
    },
    invokeApp1Router: function  (subroute) {
        if(!MyApp.Routers.App1){
            MyApp.Routers.App1 = new MyApp.App1.Router("app1/");
        }               
    },

    app2: function  () {
        console.log("app2");
        $(".app").html($("<h1 />", {text: "App2"}));
    }
});

$(document).ready(function  () {
    new MyApp.Router();
    Backbone.history.start();
})
var MyApp={};
MyApp.App1={
路由器:主干.subcure.extend({
路线:{
“”:“初始化”,
“sub1”:“sub1”
},
init:函数(){
控制台日志(“app1”);
$(“.app”).html($(“”,{text:“App1”}));
$(“.app”).append($(“”,{href:#app1/sub1”,文本:“sub1”});
},
sub1:函数(){
控制台日志(“sub1”);
$(“.app”).append($(“”,{text:{sub1}));
}
})
};
MyApp.Router=Backbone.Router.extend({
初始化:函数(){
如果(!MyApp.Routers){
MyApp.Routers={};
}
},
路线:{
“app1/*子例程”:“invokeApp1Router”,
“app2”:“app2”
},
invokeApp1Router:函数(子例程){
如果(!MyApp.Routers.App1){
MyApp.Routers.App1=新的MyApp.App1.Router(“App1/”);
}               
},
附录2:函数(){
console.log(“app2”);
$(“.app”).html($(“”,{text:“App2”}));
}
});
$(文档).ready(函数(){
新的MyApp.Router();
Backbone.history.start();
})