gpt4 book ai didi

Backbone.js 路由器事件绑定(bind)未触发

转载 作者:行者123 更新时间:2023-12-04 13:52:22 25 4
gpt4 key购买 nike

我正在尝试关注 Organizing your application using Modules (require.js我很难理解路由是如何工作的。

我无法获得简单的绑定(bind)来为索引工作:

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

加载页面时没有任何 react 。然而,这有效:
// 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
};
});

我错过了什么吗?

最佳答案

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


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 路由器事件绑定(bind)未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com