我已经实现在一个jQuery的发布/订户设计图案的一种形式。我基本上建立在Javascript中使用CoffeeScript中类,作为我的页面上的组件。即导航,DataList的等

而不必DOM元素火灾事件,我有一个对自己使用触发器来发送自定义事件,这些类的实例。然后,这些实例可以相互倾听,可以更新他们自己相应的根据每个人的行为改变的DOM元素!

我知道这个作品,因为我有我的部件正常调度自定义事件之一。不过,我遇到了一个障碍。我已经创造了另一个组成部分,也是我的生活我想不通为什么它的事件不会被解雇。

这是我的类的实现:

window.List = (function() {
    List = function(element, settings) {
      var _a, _b, _c;
      this.list = $(element);
      this.settings = jQuery.extend(List.DEFAULTS, settings);
      this.links = this.list.find(this.settings.link_selector);
      this.links.selectable();
      _b = [SelectableEvent.COMPLETED, SelectableEvent.UNDONE, SelectableEvent.SELECTED, SelectableEvent.DESELECTED];
      for (_a = 0, _c = _b.length; _a < _c; _a++) {
        (function() {
          var event_type = _b[_a];
          return this.links.bind(event_type, __bind(function(event, selectable_event) {
            return this.dispatch(selectable_event);
          }, this));
        }).call(this);
      }
      return this;
    };
    List.DEFAULTS = {
      link_selector: "a",
      completed_selector: ".completed"
    };
    List.prototype.change = function(mode, previous_mode) {
      if (mode !== this.mode) {
        this.mode = mode;
        if (previous_mode) {
          this.list.removeClass(previous_mode);
        }
        return this.list.addClass(this.mode);
      }
    };
    List.prototype.length = function() {
      return this.links.length;
    };
    List.prototype.remaining = function() {
      return this.length() - this.list.find(this.settings.completed_selector).length;
    };
    List.prototype.dispatch = function(selectable_event) {
      $(this).trigger(selectable_event.type, selectable_event);
      return alert(selectable_event.type);
    };
    return List;
}).call(this);

要注意:

List.prototype.dispatch = function(selectable_event) {
    $(this).trigger(selectable_event.type, selectable_event);
    return alert(selectable_event.type);
};

此代码被适当地触发,并且经由警报返回预期的事件类型。但警报之前,预计到自身触发一个自定义事件。这是我遇到了我的问题。

$(document).ready(function() {
    var list_change_handler, todo_list;
    todo_list = new List("ul.tasks");
    list_change_handler = function(event, selectable_event) {
      return alert("Hurray!");
    };
    $(todo_list).bind(SelectableEvent.COMPLETED, list_change_handler);
    $(todo_list).bind(SelectableEvent.UNDONE, list_change_handler);
    $(todo_list).bind(SelectableEvent.SELECTED, list_change_handler);
    $(todo_list).bind(SelectableEvent.DESELECTED, list_change_handler);
}

您在这里看到的警报“万岁”是什么,我想火但不幸的是我在这里没有运气。讽刺的是我已经做了另一大类同样的事情用相同方式实现调度自定义事件和监听器接收它就好了。任何想法为什么会不工作?

更新

每在评论中讨论,它看起来像记录“这个”控制台返回表示类的JS对象。但记录“$(本)”返回一个空的jQuery对象,从而触发永远不会被解雇。为什么$有什么想法(这)快到了空当“这个”准确地返回类的实例?

有帮助吗?

解决方案

我发现jQuery的不能索引我的对象,因为该类实现它自己的一个jQuery方法的版本。在这种情况下,长度()。重命名的长度()方法来总()解决了问题完全和类的任何实例都可以成功地触发事件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top