11

私は最近、本を読んで Backbonejs を学び始めました。そして、この問題について少し混乱しています。ルーターは次のとおりです。

define(['views/index', 'views/login'], function(indexView, loginView) {

    var SelinkRouter = Backbone.Router.extend({

        currentView: null,

        routes: {
            'home': 'home',
            'login': 'login'
        },

        changeView: function(view) {
            if(null != this.currentView)
                this.currentView.undelegateEvents();
            this.currentView = view;
            this.currentView.render();
        },

        home: function() {
            this.changeView(indexView);
        },

        login: function() {
            this.changeView(loginView);
        }
    });

    return new SelinkRouter();
});

そして、これはアプリケーションの起動方法です:

define(['router'], function(router) {

    var initialize = function() {
        // Require home page from server
        $.ajax({
            url: '/home',          // page url
            type: 'GET',           // method is get
            dataType: 'json',      // use json format
            success: function() {  // success handler
                runApplicaton(true);
            },
            error: function() {    // error handler
                runApplicaton(false);
            }
        });
    };

    var runApplicaton = function(authenticated) {

        // Authenticated user move to home page
        if(authenticated) window.location.hash='home';
                          //router.navigate('home', true); -> not work

        // Unauthed user move to login page
        else window.location.hash='login';
             //router.navigate('login', true); -> not work

        // Start history
        Backbone.history.start();
    }

    return {
        initialize: initialize
    };
});

runApplicationパートについて質問です。私が読んだ本の例では、このようにルーターをモジュールに渡しましたが、それは を使用window.location.hash = "XXX"しており、ルーターにはまったく触れていませんでした。

「navigate」メソッドでブラウザが指定したページに移動すると思ったのですが、何も起こりませんでした。なんで?

また、ベスト プラクティスとして、ページ (またはビュー) 間の移動を実現する最善の方法は何ですか?

アイデアをありがとう。

4

2 に答える 2