“路线变化”事件Backbone.js的历史? [英] Backbone.js history 'on route change' event?

查看:215
本文介绍了“路线变化”事件Backbone.js的历史?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个触发间隔为我们导航到不同的URL时间一般事件?

Is there a general event that fires every time we navigate to a different URL?

window.App =
  Models: {}
  Collections: {}
  Views: {}
  Routers: {}

  init: ->
    # Initialize Routers
    new App.Routers.Main()

    # Initialize History
    Backbone.history.start(pushState: true)

    # BIND VIEW CHANGE?
    $(@).on 'changeOfRoute', -> 
      console.log "Different Page" 

$(document).ready ->
  App.init()

按次这样做是可能的,但我在寻找一个通用的解决方案。

Doing this per view is possible, but I'm looking for a general solution.

推荐答案

有是路由器的路线事件:

There is the "route" event on the Router:

http://backbonejs.org/#Events-catalog


      
  • 路线(路由器,路由,则params) - 历史(或路由器)当任何路由已经匹配触发

  •   

这允许你绑定到特定的路由。

This allows you to bind to specific routes.

如果你想火处理程序的之后的任何路线,绑定到路线,路径将是第一个参数:

If you want to fire a handler after any route, bind to "route", and the route will be the first argument:

myRouter.on("route", function(route, params) {
    console.log("Different Page: " + route);
});

这只会触发你的明确定义的路由事件。如果要触发未明确定义的路由事件,再加入按<一个一个图示路线href=\"http://stackoverflow.com/questions/6489141/solved-how-to-detect-invalid-route-and-trigger-function-in-backbone-controller/6522963#6522963\">How检测Backbone.Controller 无效的路由和触发功能

This will only trigger events for your explicitly defined routes. If you want to trigger events for routes that are not explicitly defined, then add a 'splat' route as per How to detect invalid route and trigger function in Backbone.Controller

这篇关于“路线变化”事件Backbone.js的历史?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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