简体   繁体   English

视图上的骨干重绑定事件

[英]Backbone rebinding events on a view

I have two views, one represents a view of clients and the other is the individual client views. 我有两个视图,一个代表客户端视图,另一个代表个人客户端视图。 I am binding the mouseenter and mouseleave events in the client view to fade in and out an overlay on the image. 我在客户端视图中绑定了mouseentermouseleave事件,以淡入和淡出图像上的叠加层。 This works fine when it is by itself. 它本身就可以正常工作。 However, I am also using a jQuery plugin to do a carousel effect (plugin here ). 但是,我也使用jQuery插件来做旋转木马效果(插件在这里 )。 Once that is enabled, my custom events no longer work. 启用后,我的自定义事件将不再有效。 Is there any way I can delegate the Client View events after the plugin is initialized? 在初始化插件后,有什么方法可以委托客户端视图事件吗? This is my first time using Backbone so I might be doing something else wrong as well. 这是我第一次使用Backbone,所以我可能也会做其他错误。

Here is the code: 这是代码:

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

EDIT : Here I am trying to delegateEvents() on the views that were created (the ones that have the events on them): 编辑 :这里我试图在创建的视图(具有事件的视图delegateEvents()delegateEvents()事件():

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

Tried this but doesn't seem to work? 试过这个,但似乎没有用? Am I doing it right? 我做得对吗? I think that the plugin is doing more than I think to the DOM. 我认为插件比DOM更多。 It looks like it is touching the elements I am trying to bind to as well as binding to mouseenter and mouseleave . 看起来它正在触及我试图绑定的元素以及绑定到mouseentermouseleave I am unfamiliar with this plugin, doesn't look like it has an unminified version so I can't read it too well. 我对这个插件不熟悉,看起来它没有一个未经说明的版本,所以我读不太清楚。

Any other suggestions? 还有其他建议吗?

you can do that using delegateEvents method of your view to rebind your events 您可以使用视图的delegateEvents方法重新绑定事件

usage: myView.delegateEvents() 用法: myView.delegateEvents()

reference the documentation http://backbonejs.org/#View-delegateEvents for more info 有关详细信息,请参阅文档http://backbonejs.org/#View-delegateEvents

edit: 编辑:

this plugin binds and unbinds mouseenter/leave without namespacing - open the plugin script and add namespace to the event binding and unbinding. 此插件在没有命名空间的情况下绑定和取消绑定mouseenter / leave - 打开插件脚本并将命名空间添加到事件绑定和解除绑定。

apply these fixes to every occurence of these and it should be ok even without the delegateEvents() 将这些修复应用于每次出现这些修复,即使没有delegateEvents()也应该没问题

r.unbind("mouseenter"); => r.unbind("mouseenter.carousel"); => r.unbind("mouseenter.carousel"); r.unbind("mouseleave"); => r.unbind("mouseleave.carousel"); => r.unbind("mouseleave.carousel");

r.mouseenter(function() { ... => r.bind('mouseenter.carousel', function() { ... r.mouseleave(function() { ... => r.bind('mouseleave.carousel', function() { ... r.mouseenter(function() { ... => r.bind('mouseenter.carousel', function() { ... r.mouseleave(function() { ... => r.bind('mouseleave.carousel', function() { ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM