-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01df682
commit bf5fd7f
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Attach.js v1.0.2 | ||
* Attaches JavaScript to HTML without messy selectors. | ||
* | ||
* https://github.com/nicbell/attach.js | ||
* MIT License | ||
* Copyright 2013 Nic Bell | ||
*/ | ||
|
||
;(function (Attach) { | ||
Attach.engine = null; | ||
|
||
Attach.items = []; | ||
|
||
Attach.add = function (id, fn) { | ||
this.items.push({ id: id, func: fn }); | ||
}; | ||
|
||
Attach.remove = function (id) { | ||
for (var i = this.items.length; i--;) { | ||
if (this.items[i].id === id) { | ||
this.items.splice(i, 1); | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
Attach.run = function () { | ||
var elements = this.engine ? this.engine('[data-attach]') : document.querySelectorAll('[data-attach]'); | ||
|
||
for (var i = 0; i < elements.length; i++) { | ||
this._attach(elements[i]); | ||
} | ||
}; | ||
|
||
Attach._attach = function (element) { | ||
var ids = element.getAttribute('data-attach').split(' '); | ||
|
||
for (var i = 0; i < this.items.length; i++) { | ||
if (ids.indexOf(this.items[i].id) >= 0 && typeof this.items[i].func === 'function') { | ||
this.items[i].func.call(this, element); | ||
} | ||
} | ||
}; | ||
})(window.Attach = window.Attach || {}); |