Backbone.js的路由器事件绑定不触发 [英] Backbone.js Router event binding not firing

查看:97
本文介绍了Backbone.js的路由器事件绑定不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照使用模块组织你的应用程序(require.js 的我挣扎了解路由的作品。

I'm trying to follow Organizing your application using Modules (require.js I'm struggling to understand how routing works.

我不能让简单的绑定到索引工作:

I cannot get simple binding to work for index:

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

在页面加载没有任何反应。然而,这作品:

When page is loaded nothing happens. This however works:

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

我缺少的东西吗?

Am I missing something?

推荐答案

好了,所以这是它是如何做的:

Ok, so this is how it's done:


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

        app_router.on("route:index", function () {
            alert("hello world");
        });

        Backbone.history.start();

        return app_router;
    };

这篇关于Backbone.js的路由器事件绑定不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆