読み書きプログラミング

日常のプログラミングで気づいたことを綴っています

Iron Routerとsubscribe

マルチページアプリの場合、通常ページ毎に扱うデータベースが異なるので、Iron RouterではbeforeかwaitOnを使ってsubscribeすることになります。
データ量の大きなものをアクセスする場合にはwaitOnを使うと表示まで時間が掛かるので、beforeを使ってリアクティブに更新するのが進捗が見えていいです。
ところが、この場合、更新中に別のページに遷移すると、遷移前のページでのsubscribeが完了するまでページ遷移が待たされるようになります。
これがとても印象が悪い。
なので、ページ遷移の際にsubscribeを止めるようにしたところ、快適になりました。

subscribed = null

Router.map ->
    @route 'home',
        path: '/'
        template: 'home'
        before: ->
            subscribed.stop() if subscribed? and not subscribed.ready()
            subscribed = @subscribe('records', 'pickup').wait() #小さなデータ

    @route 'game-list',
        path: '/game-list'
        template: 'game-list'
        before: ->
            subscribed.stop() if subscribed? and not subscribed.ready()
            subscribed = @subscribe 'records' # 大きなデータ