【问题标题】:trying to hide dropdown in backbone error试图隐藏主干错误中的下拉菜单
【发布时间】:2015-07-21 02:15:01
【问题描述】:

notification_dropdown_view.js:

initialize: function(){
$(document.body).click($.proxy(this.hideDropdown, this));

this.notifications = [];
this.perPage = 5;
this.hasMoreNotifs = true;
this.badge = this.$el;
this.dropdown = $("#notification-dropdown");
this.dropdownNotifications = this.dropdown.find(".notifications");
this.ajaxLoader = this.dropdown.find(".ajax_loader");
this.perfectScrollbarInitialized = false;
},    

hideDropdown: function(evt){
var inDropdown = $(evt.target).parents().is($(".dropdown-menu", this.dropdown));
var inHovercard = $.contains(app.hovercard.el, evt.target);
if(!inDropdown && !inHovercard && this.dropdownShowing()){
  this.dropdown.removeClass("dropdown-open");
  this.destroyScrollbar();
}
}

header_view.js:

 app.views.Header = app.views.Base.extend({

 templateName: "header",

 className: "dark-header",

  events: {
 "focusin #q": "toggleSearchActive",
 "focusout #q": "toggleSearchActive"
 },

 presenter: function() {
 return _.extend({}, this.defaultPresenter(), {
  podname: gon.appConfig.settings.podname
 });
},

  postRenderTemplate: function(){
  new app.views.Notifications({ el: "#notification-dropdown" });
  this.notificationDropdown = new app.views.NotificationDropdown({ el: "#notification-dropdown" });
new app.views.Search({ el: "#header-search-form" });
  },

  menuElement: function(){ return this.$("ul.dropdown"); },

  toggleSearchActive: function(evt){
   // jQuery produces two events for focus/blur (for bubbling)
   // don't rely on which event arrives first, by allowing for both variants
   var isActive = (_.indexOf(["focus","focusin"], evt.type) !== -1);
  $(evt.target).toggleClass("active", isActive);
  return false;
  }
 });

在 RoR 应用程序中,当同时单击一个图标时,会打开和关闭一个下拉菜单以显示通知。 hideDropdown 应该在打开下拉菜单时隐藏它,但它没有,我收到错误:

未捕获的类型错误:无法读取未定义的属性“el”

我认为它与“this”有关。任何人都可以帮忙吗?

【问题讨论】:

  • 您可以在 chrome 中获取错误的堆栈跟踪,然后让我们知道究竟是哪一行抛出了错误。更好的是,在该行上放一个断点并遍历堆栈上方以查看元素未定义的原因。
  • 这将是我的第一次调试。在阅读了您所写的内容和一些研究之后,我已经掌握了基本原理。我想知道它是如何在 Rails 应用程序中完成的,有些人使用 debugger gem for rails。我想知道哪个适合这里的主干?

标签: javascript jquery ruby-on-rails function backbone.js


【解决方案1】:

您是否已将 el 分配给视图?喜欢:

headerView = new App.Views.HeaderView
  el: '#header'

你可以为你的视图指定一个选择器 el http://backbonejs.org/#View-el

【讨论】:

  • 不,这个类被称为 notification_dropdown_view.js 并且与下拉列表中的通知呈现有关。还有一个类叫header_view.js 会更新大家看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
相关资源
最近更新 更多