2017-06-13 5 views
1

私は、バックボーンとマリオネットを使って簡単なアプリケーションを作成しています。それは単に(APIを使用して)Wordpressの投稿のリストを取得し、それを表示することです。 非常にシンプルなアプリなので、モジュール化されていません。バックボーン・マリオネットが発射経路を持っていない

if (Backbone.history) 
    Backbone.history.start({ pushState: false }); 

if (Backbone.history.fragment === '') 
    API.listAllPosts(); 
else 
    API.listSinglePost(Backbone.history.fragment); 


// Is not firing anything from here... 
MyBlog.Router = Marionette.AppRouter.extend({ 
    appRoutes: { 
     '': 'listPosts', 
     ':post_name': 'listSingle' 
    }, 
    listPosts: function() { 
     console.log('router'); 
     API.listAllPosts(); 
    }, 
    listSingle: function(model) { 
     console.log('router, single'); 
     API.listSinglePost(model); 
    } 
}); 
// ...to here 

var API = { 
    listAllPosts: function() { 
     // Fetch all posts and display it. It's working 
    }, 
    listSinglePost: function(model) { 
     // Fetch a single post and display it. It's working 
    } 
} 

MyBlog.addInitializer(function() { 
    console.log('initializer'); // It's firing 

    new MyBlog.Router({ 
     controller: API 
    }); 
}); 

Derick Bailey, Marionette's creator, saidとしてnaviagate上のトリガの使用について:

私は(それがすべて同じファイル内に置かれています)以下の持っている

それは悪いアプリのデザインを奨励し、それが強く、あなたをお勧めします をtrigger:trueからBackbone.history.navigateに渡すことはできません。

ここに何がありますか?

答えて

0

あなたは、ルータのインスタンスを作成する前に、バックボーンの歴史を開始します。

ルータを作成した後に移動してください。

MyBlog.addInitializer(function() {  
    new MyBlog.Router({ controller: API }); 

    // should be started after a router has been created 
    Backbone.history.start({ pushState: false }); 
}); 

他の事は、コールバックshould be defined inside of a controllerたり、routesappRoutesを変更する必要があることです。

appRoutesroutesの主な違いは、私たちは、コントローラに直接ではなく、ルータ自体に コールバックを提供するということです。 [...] AppRouterBackbone.Routerに拡張されているため、AppRouterにコールバックが存在する必要があるroutes 属性を定義することもできます。

+1

@Emile Bergeronありがとう!それは私の投稿リストを表示して戻ってナビゲートしています。問題は、ルートの代わりにappRoutesを使用していたときと、それらを呼び出す方法でした。 ありがとうございます! – moreirapontocom

0

移動し、あなたのアプリケーションが起動またはinitialize:afterイベントハンドラ内部に入った時点の後、この

if (Backbone.history) 
    Backbone.history.start({ pushState: false }); 

if (Backbone.history.fragment === '') 
    API.listAllPosts(); 
else 
    API.listSinglePost(Backbone.history.fragment); 

に。

チェックこの前の質問:Marionette.js appRouter not firing on app start

+0

私は試しましたが...運はありません。 – moreirapontocom

+0

あなたのリンクの 'href'にハッシュ('# ')を使っていますか? – vassiliskrikonis

+0

History.navigate()はリンクでのみ動作しますか? 実際に私はリンクを使用していません。順序付けられていないリストがあり、各li要素にはclickイベントが付いたボタンがあります。クリックすると、Backbone.History.navigate( 'proper-link-here')という関数が呼び出されます。 – moreirapontocom

関連する問題