Skip to content

Commit

Permalink
Added AMD, CommonJS and Node module support bumped version to 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic Bell committed Jul 30, 2015
1 parent b97329b commit 73e3e8d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Attach.js.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Attach.js</id>
<version>1.0.3</version>
<version>1.0.4</version>
<authors>Nic Bell</authors>
<projectUrl>http://nicbell.github.io/attach.js/</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand Down
75 changes: 48 additions & 27 deletions attach.js
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);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Attach.js",
"version": "1.0.3",
"version": "1.0.4",
"description": "Attaches JavaScript to HTML without messy selectors.",
"homepage": "http://nicbell.github.io/attach.js/",
"ignore": [
Expand Down

0 comments on commit 73e3e8d

Please sign in to comment.