2

これは私のCoffescriptルータークラスです:

class App.Router extends Backbone.Router

  initialize: ->

    console.log 'Router.init'

    @on 'all', ->
      console.info 'route changed'

  routes:
    '': 'home'
    'test': 'test'

  home: ->
    console.log 'home routed'

  test: ->
    console.log 'test routed'

ローカルホストページをリロードすると、「@ on'all'」コールバックが2回トリガーされるようです(firebugのdouble console.info ...)。

これは私のfirebugの出力です:

App.init
Session.init
Router.init
home routed
route changed
route changed

ご覧のとおり、「ルート変更」出力はホームルートの後に配置されます...

そして最後に、これは私のブートストラップコードです(私のアプリの名前空間を使用しています...)、ここにhistory.startを配置しました

App =
  init: ->
    console.log 'App.init'
    @session = new App.Model.Session
    @router = new App.Router
    # @userPanel = new App.View.UserPanel
    Backbone.history.start pushState: true

  Model: {}
  View: {}
4

2 に答える 2

5

イベントは、ソースオブジェクトによってトリガーされた任意のタイプのすべてのイベントallをキャプチャする特別なイベントリスナー構文です。他のイベントは実際にはイベントではなく、イベントだと思います。routeroute:name

すべてのルートをキャッチするには、route代わりにイベントを使用します。

@on 'route', ->
  console.info 'route changed'
于 2013-02-11T14:34:27.567 に答える
2

これは、バックボーンソースを見て説明することができます:

this.trigger.apply(this, ['route:' + name].concat(args));
this.trigger('route', name, args);

'route'イベントのみをリッスンしてみてください。

@on 'route'
于 2013-02-11T14:34:13.243 に答える