11

I began learning Backbonejs recently, by reading a book. and I feel a little bit confuse about this issue.Here is a Router:

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();
});

and this is the boot method of a application:

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
    };
});

My question is about the runApplication part. The example of the book that I read passed router into module just like this, but it used window.location.hash = "XXX", and the router wasn't touched at all.

I thought the "navigate" method would make browser move to the page I specified, but nothing happened. Why?

And for the best practice sake, what is the best way to achieve movement between pages(or views)?

thanks for any ideas.

4

2 回答 2

18

您还可以使用静态方法来避免路由器依赖(例如使用 requirejs 时)。

Backbone.history.navigate(fragment, options)

这样,您只需要:

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

// Authenticated user move to home page
if(authenticated)
  Backbone.history.navigate('home', true);
// Unauthed user move to login page
else
  Backbone.history.navigate('login', true);
于 2013-11-14T09:50:04.353 回答
12

根据文档,如果您还想调用属于特定路由的函数,则需要传递选项trigger: true

每当您在应用程序中到达想要保存为 URL 的点时,调用 navigate 以更新 URL。如果您还想调用路由函数,请将触发器选项设置为 true。要更新 URL 而不在浏览器的历史记录中创建条目,请将替换选项设置为 true。

您的代码应如下所示:

if(authenticated)
    router.navigate('home', {trigger: true});

创建路由器后,您还必须调用

Backbone.history.start();

Backbone.history.start([选项])

当您的所有路由器都已创建并正确设置所有路由后,调用 Backbone.history.start() 开始监控 hashchange 事件并调度路由。

最后 runApplication 逻辑将与此类似:

var runApplicaton = function(authenticated) {

    var router = new SelinkRouter();
    // Start history
    Backbone.history.start();

    // Authenticated user move to home page
    if(authenticated)
        router.navigate('home', true);
    // Unauthed user move to login page
    else
        router.navigate('login', true);
}
于 2013-04-18T06:40:04.000 回答