Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干重新绑定视图上的事件_Javascript_Backbone.js - Fatal编程技术网

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