6

モジュールを使用してアプリケーションを整理しようとしています (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
    };
});

何か不足していますか?

4

1 に答える 1

11

わかりました、これはそれがどのように行われるかです:


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

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

        Backbone.history.start();

        return app_router;
    };
于 2012-11-27T11:28:22.883 に答える