2017-03-23 11 views
0

バックボーンルータに次のコードがあると、ユーザが#test/mapまたは#test/imagesに直接ナビゲートすると、最初にルート#testに接続された機能が実行されることを確認できますか?バックボーン - サブルートの機能の前にルート機能を実行

invokeTest関数は、サブビュー "マップ"と "イメージ"がレンダリングされるコンテナを持つ親ビューを作成します。だから私は基本レイアウトビューは、サブビューをレンダリングする前にレンダリングされていることを確認する必要があります。

var Workspace = Backbone.Router.extend({ 
 
    routes: { 
 
    "test":     "invokeTest", // #test 
 
    "test/map":  "invokeTestMap", // #test/map 
 
    "test/images": "invokeTestImages" // #test/images 
 
    }, 
 

 
    invokeTest: function() { 
 
    //render base-layout 
 
    console.log("test function executed"); 
 
    }, 
 

 
    invokeTestMap: function() { 
 
    //render map view using element created with the base layout template 
 
    console.log("Map function executed"); 
 
    }, 
 
    invokeTestImages : function(){ 
 
    //render images view using element created with the base layout template 
 
    console.log("images function executed"); 
 
    } 
 

 
});

今私はサブビューのためのコンソールログを取得し、ルート関数が呼び出されることはありません。

答えて

1

あなただけ行うことができます。

invokeTest: function() { 
    //render base-layout 
    createBaseLayout(); 
    console.log("test function executed"); 
    }, 

    invokeTestMap: function() { 
    createBaseLayout(); 
    //render map view using element created with the base layout template 
    console.log("Map function executed"); 
    }, 
    invokeTestImages : function(){ 
    createBaseLayout(); 
    //render images view using element created with the base layout template 
    console.log("images function executed"); 
    } 

をそれとも、

invokeTestMap: function() { 
    this. invokeTest(); 
    //render map view using element created with the base layout template 
    console.log("Map function executed"); 
    }, 

ようにそれを行うことができます単にあなたが関数内で再利用するためのロジックを入れて、それを呼び出す必要があります置きます。

ルーターのinitializeでこれを行う必要がある場合は、regexを使用して親子関係を特定し、子コールバックを親コールバックも同様にラップする関数に更新できます。 (あるいはルータにもっと深く入ることができます - backbone.subrouteのような同様の問題に取り組むプラグインがあるようです。

+0

私はベースレイアウトをレンダリングするために 'initialize'メソッドを使用していました。今、私はログインビューを持っているので、私はもうこのアプローチを使用することはできません。あなたの2番目の提案は、ルータがベースレイアウトビューへの参照を持っていて、すでに存在しない場合にのみ – randomguy04

+0

@ randomguy04はい、一般的にはどのビューもルータで初期化されているのを見たことがあります –

+0

このようにして、うまくいきましたが、私は電話をしなければならないので、この新しいメソッドは、baselayoutがそれに依存するすべてのビューに存在するかどうかをチェックします。参照のためには、これを 'execute'メソッドで使用することはできません。ルートのナビゲーション)私のアプリのすべてのビューがこのbaselayoutを必要とするわけではなく、ログインビューがあります。 – randomguy04

関連する問題