Javascript 主干重新绑定视图上的事件
我有两个视图,一个代表客户的视图,另一个代表单个客户的视图。我正在绑定客户端视图中的Javascript 主干重新绑定视图上的事件,javascript,backbone.js,Javascript,Backbone.js,我有两个视图,一个代表客户的视图,另一个代表单个客户的视图。我正在绑定客户端视图中的mouseenter和mouseleave事件,以淡入淡出图像上的覆盖。当它单独工作时,效果很好。然而,我也在使用jQuery插件来实现旋转木马效果(插件)。一旦启用,我的自定义事件将不再工作。在插件初始化之后,我是否可以委托客户端视图事件?这是我第一次使用主干网,所以我可能还做了其他错误的事情 代码如下: // Client View window.ClientView = Backbone.View.exte
mouseenter
和mouseleave
事件,以淡入淡出图像上的覆盖。当它单独工作时,效果很好。然而,我也在使用jQuery插件来实现旋转木马效果(插件)。一旦启用,我的自定义事件将不再工作。在插件初始化之后,我是否可以委托客户端视图事件?这是我第一次使用主干网,所以我可能还做了其他错误的事情
代码如下:
// Client View
window.ClientView = Backbone.View.extend({
tagName: 'li',
template: _.template($("#client-template").html()),
className: 'client-thumb',
events: {
"mouseenter": "fadeOutOverlay",
"mouseleave": "fadeInOverlay"
},
initialize: function() {
},
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
fadeOutOverlay: function() {
$(this.el).find(".slider-image-overlay").fadeOut('fast');
},
fadeInOverlay: function() {
$(this.el).find(".slider-image-overlay").fadeIn('fast');
}
});
// Clients View
window.ClientsView = Backbone.View.extend({
el: "#clients",
initialize: function() {
this.collection.bind('all', this.render, this);
},
render: function() {
var $clients = $("<ul class='clearfix'></ul>");
_.each(this.collection.models, function(client) {
var view = new ClientView({model: client});
$clients.append(view.render().el);
});
$(this.el).hide().append($clients).fadeIn().scrollingCarousel();
return this;
}
});
试过这个但似乎不起作用?我做得对吗?我认为插件对DOM的作用比我想象的要大。它似乎正在接触我试图绑定的元素以及绑定到mouseenter
和mouseleave
的元素。我不熟悉这个插件,看起来它没有一个未统一的版本,所以我看不太清楚
还有其他建议吗?您可以使用视图的
delegateEvents
方法重新绑定事件
用法:myView.delegateEvents()
有关更多信息,请参阅文档
编辑:
此插件绑定和解除绑定mouseenter/leave而不使用名称空间-打开插件脚本并将名称空间添加到事件绑定和解除绑定中
将这些修复应用于这些事件的每次发生,即使没有delegateEvents()
r.unbind(“鼠标指针”)代码>=>r.unbind(“mouseenter.carousel”)代码>
r.unbind(“mouseleave”)代码>=>r.unbind(“mouseleave.carousel”)代码>
r.mouseenter(function(){…
=>r.bind('mouseenter.carousel',function(){…
r.mouseleave(function(){…
=>r.bind('mouseleave.carousel',function(){…
可能是我错了,但是您没有将任何事件附加到您的客户端。如果您没有将选择器传递给事件对象,则事件绑定到视图el
ementI我用试图delegateEvents的编辑更新了我的答案()
但仍然不起作用。可能与插件有关…谢谢你的帮助。不起作用,插件肯定做得比我想象的要多。我现在只想在afterCreate回调中使用常规jQuery委托。
App.View.ClientsView = Backbone.View.extend({
el: "#clients",
initialize: function() {
this.collection.bind('all', this.render, this);
},
render: function() {
var $clients = $("<ul class='clearfix'></ul>");
var views = [];
_.each(this.collection.models, function(client) {
var view = new App.View.ClientView({model: client});
views.push(view); // Store created views in an array...
$clients.append(view.render().el);
});
$(this.el).hide().append($clients).fadeIn().scrollingCarousel({
// Use the plugin's callback to try to delegate events again
afterCreateFunction: function() {
_.each(views, function(view){
view.delegateEvents();
});
}
});
return this;
}
});