jQuery:このトリガーは火災からJSす。
-
25-09-2019 - |
質問
っての実施形態の出版社/加入デザインパターンマウスでクリック.私は基本的にビルの授業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を表すオブジェクトのクラスです。でログイン"$(this)"を返す空のjQueryオブジェクトがトリガーになりません。意思いのはなぜ$(this)は空の場合"本"を正確に復帰、クラスのインスタンス?
解決
私はjQueryのクラスはjQueryのメソッドのそれ自身のバージョンを実装することができませんインデックス、私のオブジェクトので、ということが分かりました。この場合、長さ()。 )(合計長さ()メソッドの名前を変更する問題を完全に解決され、クラスのインスタンスが正常にトリガ・イベントができます。