您的问题来自实施的openProfile
方法。
您正在使用的profileView
有实例,你在什么地方初始化像
var profileView = new ProfileView();
ProfileView
延伸从Backbone.View
,当你初始化它会delegate events 并将其绑定到this.$el
。
当您在this.$el
上调用jQuery的remove()
方法时,它会将其删除并解除所有连接事件的绑定。
下一次当你打电话给openProfile
时,profileView.render().$el
会返回你的看法,但没有任何事件。
为了避免这种情况,您需要重构代码。有几种情况可以实现此任务。其中之一是始终使用的ProfileView
新实例,如:
events: {
'click .toolbar_ship': 'openProfile'
},
openProfile: function() {
var profileView = new ProfileView();
gameView.$el.append(profileView.render().$el);
}
和ProfileView:
events: {
'click .object_in_inventory': 'inventoryClick',
'click .object_in_cell': 'cellClick',
'click .close_profile': 'closeProfile'
},
render: function() {
this.$el.html(this.template());
return this;
},
closeProfile: function() {
this.remove(); // this.remove() is the method of Backbone.View, which will manage removing of view and unbinding of events.
}
另一种解决方案可以只隐藏个人资料视图当关闭个人
用户点击
events: {
'click .toolbar_ship': 'openProfile'
},
openProfile: function() {
if (this.profileView) {
this.profileView.$el.show(); // your custom showing logic
} else {
this.profileView = new ProfileView(); // caching profileView
gameView.$el.append(profileView.render().$el);
}
}
and ProfileView中:
events: {
'click .object_in_inventory': 'inventoryClick',
'click .object_in_cell': 'cellClick',
'click .close_profile': 'closeProfile'
},
render: function() {
this.$el.html(this.template());
return this;
},
closeProfile: function() {
this.$el.hide(); // your custom showing logic
}
不要忘记管理ProfileView删除和事件解除绑定,当你不再需要它。
控制台中是否有错误? – 2014-12-05 10:28:45