简体   繁体   English

每条路线上都有骨干路由器

[英]Backbone router on every route

My problem is: 我的问题是:

All the HTML is in a <div id="application"> container, which by default has 0 opacity. 所有HTML都在<div id="application">容器中,默认情况下为0不透明度。 This is because JavaScript does some DOM positioning, so when this is all done I just add a class="rendered" into the <div id="application>" and then the page appears. 这是因为JavaScript做了一些DOM定位,所以当这一切都完成后我只需要在<div id="application>"添加一个class="rendered" <div id="application>" ,然后就会出现页面。

So the thing is, for every route in my Backbone router, I need something that is always triggered before the actual routing, or on each route, is this possible? 所以问题是,对于我的Backbone路由器中的每条路由,我需要在实际路由之前始终触发的东西,或者在每条路由上,这可能吗?

Thanks for help! 感谢帮助!

Backbone.Router emits events when a route triggers, so you can either bind to specific events or to all to capture all valid route changes. Backbone.Router在路由触发时发出事件,因此您可以绑定到特定事件或all捕获所有有效的路由更改。 The event argument will contain the route callback name: event参数将包含路由回调名称:

var MyRouter = Backbone.Router.extend({
    routes: {
        "foo": "bar"
    },

    initialize: function() {
        this.on('all', function(routeEvent) {
            // If you had clicked #foo, routeEvent would contain
            // `route:bar`. You can trigger your CSS changes here.
        });
    }
});

I feel like this sort of code would be better off in a view, but this ought to do what you ask for. 我觉得这种代码在视图中会更好,但这应该按照你的要求做。

You could define one method in your router that takes a parameter. 您可以在路由器中定义一个带参数的方法。 Then for every route, call that method and pass in one argument that will be used to determine which view to render. 然后,对于每个路由,调用该方法并传入一个参数,该参数将用于确定要呈现的视图。 At the beginning of the method you could define whatever "on-every-router-call" logic you want to happen. 在方法开始时,您可以定义要发生的“每个路由器调用”逻辑。

For example: 例如:

routes:
    "": "renderPage"
    ":page": "renderPage"


renderPage: (page) ->
// check if page is defined, if not render index, if it is, render that page

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM