【问题标题】:Backbone.js Router event binding not firingBackbone.js 路由器事件绑定未触发
【发布时间】:2012-11-27 11:14:07
【问题描述】:

我正在尝试关注 Organizing your application using Modules (require.js 我正在努力理解路由的工作原理。

我无法让简单的绑定为索引工作:

// Filename: router.js
define([
  'jquery',
  'underscore',
  'backbone',
  'views/projects/list'
], function ($, _, Backbone, ProjectListView) {
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
            '': 'index'
        }
    });

    var initialize = function () {
        var app_router = new AppRouter();

        app_router.on('index', function () {
            alert("index"); // this never gets called
        });

        Backbone.history.start();

        return app_router;
    };
    return {
        initialize: initialize
    };
});

当页面被加载时,什么也没有发生。然而这有效:

// Filename: router.js
define([
  'jquery',
  'underscore',
  'backbone',
  'views/projects/list'
], function ($, _, Backbone, ProjectListView) {
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
            '': 'index'
        },
        index: function() { alert("works"); }
    });

    var initialize = function () {
        var app_router = new AppRouter;

        Backbone.history.start();

        return app_router;
    };
    return {
        initialize: initialize
    };
});

我错过了什么吗?

【问题讨论】:

    标签: backbone.js


    【解决方案1】:

    好的,这就是它的完成方式:

    
        var initialize = function () {
            var app_router = new AppRouter();
    
            app_router.on("route:index", function () {
                alert("hello world");
            });
    
            Backbone.history.start();
    
            return app_router;
        };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2012-04-16
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多