Javascript 主干侦听主干。历史事件
我正在使用主干+requirejs构建我的应用程序,在下面的模块中,初始化函数用于从另两个名为“HomeView,InnerView”的模块加载初始化函数,在这个模块中,我要侦听Backbone.history事件。但是模块没有监听这些事件 路由器Javascript 主干侦听主干。历史事件,javascript,backbone.js,requirejs,js-amd,Javascript,Backbone.js,Requirejs,Js Amd,我正在使用主干+requirejs构建我的应用程序,在下面的模块中,初始化函数用于从另两个名为“HomeView,InnerView”的模块加载初始化函数,在这个模块中,我要侦听Backbone.history事件。但是模块没有监听这些事件 路由器 // Filename: router.js define([ 'jquery', 'underscore', 'backbone', 'app/config', 'app/views/homeview', 'app/views/inner_view
// Filename: router.js
define([
'jquery',
'underscore',
'backbone',
'app/config',
'app/views/homeview',
'app/views/inner_view'
], function($, _, Backbone, Config, HomeView, InnerView) {
var AppRouter = Backbone.Router.extend({
initialize: function() {
},
routes: {
// Define some URL routes
'': 'defaultRoute',
'!/home': 'defaultRoute',
'!/activities': 'activities',
'!/activities/:id': 'activity',
'!/vacancies': 'vacancies',
'!/vacancies/:id': 'vacancies',
// 404, not found
'*actions': '404'
},
defaultRoute: function() {
//if this route is served from inner pages, we have to trigger "close:Inner"
Backbone.trigger('close:Inner');
Backbone.current = 'Home';
},
activities: function(){
Backbone.trigger('view:Activities');
Backbone.trigger('close:'+Backbone.current);
Backbone.current = 'Activities';
},
activity: function(id){
Backbone.trigger('close:' + Backbone.current);
Backbone.current = 'Activity';
},
vacancies: function(){
},
404: function(actions) {
// We have no matching route, lets display 404
$('.content_wapper').html(actions + " no such page on the system");
}
});
var initialize = function() {
HomeView.initialize();
InnerView.initialize();
Backbone.appRouter = new AppRouter();
Backbone.history.start();
};
return {
initialize: initialize
};
});
是什么原因阻止我在另一个视图中收听主干历史事件?
内部视图模块
// Filename: views/boilerplate.js
define([
'jquery',
'underscore',
'backbone',
'app/config',
'text!templates/inner_header.html',
'bootbox',
'text!templates/breadcrumb.html',
'purl'
], function($, _, Backbone, Config, inner_header_template, bootbox, breadcrumbTemplate) {
var url = $.url(),
InnerView = Backbone.View.extend({
el: $(".header_content"),
template: _.template(inner_header_template),
initialize: function(options) {
Backbone.history.on('route:activities', this.activities);
},
render: function() {},
activities: function() {
alert('activities');
},
events: {},
close: function() {
$('.top_menu').remove();
$('.main_menu_inner').remove();
$('.content_container').empty();
$(this.el).unbind();
delete this.$el; //delete the jQuery wrapped object variable
delete this.el; //delete the variable reference to this node
}
});
var initialize = function() {
new InnerView();
};
return {
initialize: initialize
};
});
您正在触发主干对象上的事件,而不是主干。历史记录,请尝试以下操作:
Backbone.history.trigger('close:Inner');
您必须在路由器本身而不是主干上侦听。历史记录。这说明了这一点。我是在询问这个事件
Backbone.history.on('route:activities',this.activities')
和您提到的事件对我来说非常适合:)您在哪里触发了路由:活动
事件?在初始化为路由器模块的初始化函数的InnerView中,请原谅我,但我在您的示例中看到的是,您绑定了事件主干.history.on('route:activities',this.activities)代码>不触发它。我说的对吗?是的,我是按照主干文档做的,每个路由都会自动触发它的事件。我在路由器上测试了它,它工作得很好,但在视图上不起作用