-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AMD, CommonJS and Node module support bumped version to 1.0.4
- Loading branch information
Nic Bell
committed
Jul 30, 2015
1 parent
b97329b
commit 73e3e8d
Showing
3 changed files
with
50 additions
and
29 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
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 |
---|---|---|
@@ -1,45 +1,66 @@ | ||
/** | ||
* Attach.js v1.0.3 | ||
* Attach.js v1.0.4 | ||
* Attaches JavaScript to HTML without messy selectors. | ||
* | ||
* https://github.com/nicbell/attach.js | ||
* MIT License | ||
* Copyright 2013 Nic Bell | ||
* Copyright 2015 Nic Bell | ||
*/ | ||
|
||
;(function (Attach) { | ||
Attach.engine = null; | ||
(function (global) { | ||
'use strict'; | ||
|
||
Attach.items = []; | ||
var Attach = { | ||
engine: null, | ||
items: [], | ||
|
||
Attach.add = function (id, fn) { | ||
this.items.push({ id: id, func: fn }); | ||
}; | ||
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; | ||
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]'); | ||
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]); | ||
} | ||
}; | ||
for (var i = 0; i < elements.length; i++) { | ||
this._attach(elements[i]); | ||
} | ||
}, | ||
|
||
Attach._attach = function (element) { | ||
var ids = element.getAttribute('data-attach').split(' '); | ||
_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); | ||
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 || {}); | ||
|
||
|
||
// AMD support | ||
if (typeof define === 'function' && define.amd) { | ||
define(function () { return Attach; }); | ||
} | ||
else if (typeof exports !== 'undefined') { | ||
// Support Node.js specific 'module.exports' (which can be a function) | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = Attach; | ||
} | ||
// But always support CommonJS module 1.1.1 spec ('exports' cannot be a function) | ||
exports.Attach = Attach; | ||
} | ||
else { | ||
global.Attach = Attach; | ||
} | ||
|
||
})(this); |
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