1
var MyRouter = Backbone.Router.extend({
router : {
'something' : 'method1',
'anotherthing' : 'method2'
},
method1 : function() {
if (!this.someView)
this.someView.close()
this.someView = new SomeView();
$('someElement').append(this.someView.render().el);
},
method2 : function() {
//do something
}
});
var SomeView= Backbone.View.extend({
initialize : function() {
$(window).on('resize',_bind(this.onResize, this));
},
onResize : function() {
alert('resized');
},
close : function() {
this.remove();
this.off();
this.undelegateEvents();
}
});
以上是我的代码示例。无论何时路由器呈现视图,它都将解除绑定到现有视图的所有事件,以便将其处理,并创建一个新视图。因为我已经在窗口中附加了onResize事件,对于someView的每个实例(从#something到#anotherthing导航并返回到#something,这将取消绑定现有this.someView中的所有事件,并创建一个新的this.someView实例),它会将一个事件绑定到窗口,并且多次触发onResize方法,即使当前视图不在显示器中(即我的路由器可能会显示method2中定义的其他视图)。我明白为什么它会被触发多次,但是想知道是否有在骨干或jQuery中处理这个事件,而不是绑定事件到窗口的resize事件?手柄主干中的移动屏幕方向
P.S.我没有使用jQuery.mobile,也不想使用任何额外的JavaScript插件,因为它会增加手机浏览器的负载。