├── .gitignore
├── CHANGELOG.md
├── .jshintrc
├── tasks
├── clean.js
├── bytesize.js
├── changelog.js
├── copy.js
├── markdox.js
├── uglify.js
├── watch.js
├── jscs.js
├── mocha.js
├── bump.js
├── jshint.js
├── copyright.js
└── buildcontrol.js
├── .travis.yml
├── src
├── .jshintrc
└── events.js
├── bower.json
├── tests
├── bower.json
├── index.html
└── spec
│ └── events.js
├── .jscsrc
├── Gruntfile.js
├── README.md
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 | /build
3 | /docs
4 | .DS_Store
5 | /tests/bower_components/
6 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 |
2 | ## 0.0.0 (2014-12-31)
3 |
4 |
5 |
6 | ## 0.0.0 (2014-12-30)
7 |
8 |
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "curly": true,
3 | "eqeqeq": true,
4 | "immed": true,
5 | "latedef": true,
6 | "newcap": true,
7 | "noarg": true,
8 | "sub": true,
9 | "undef": true,
10 | "unused": "vars",
11 | "boss": true,
12 | "eqnull": true,
13 | "node": true
14 | }
15 |
--------------------------------------------------------------------------------
/tasks/clean.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('clean', {
9 | build: ['build', 'docs']
10 | });
11 | };
12 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | cache: node_modules
3 |
4 | sudo: false
5 |
6 | notifications:
7 | email:
8 | recipients:
9 | - shane@shanetomlinson.com
10 | on_success: change
11 | on_failure: always
12 |
13 | node_js:
14 | - 0.10.25
15 |
16 | install:
17 | - phantomjs --version
18 | - travis_retry npm install --silent
19 | - npm run-script setup-bower
20 |
21 | script:
22 | - npm test
23 |
24 |
--------------------------------------------------------------------------------
/tasks/bytesize.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('bytesize', {
9 | all: {
10 | src: ['build/events.js', 'build/events.min.js']
11 | }
12 | });
13 | };
14 |
--------------------------------------------------------------------------------
/tasks/changelog.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('changelog', {
9 | options: {
10 | from: 'source-<%= pkgReadOnly.version %>'
11 | }
12 | });
13 | };
14 |
--------------------------------------------------------------------------------
/tasks/copy.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('copy', {
9 | dist: {
10 | src: 'src/events.js',
11 | dest: 'build/events.js'
12 | }
13 | });
14 | };
15 |
--------------------------------------------------------------------------------
/src/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "curly": true,
3 | "eqeqeq": true,
4 | "immed": true,
5 | "latedef": true,
6 | "newcap": false,
7 | "noarg": true,
8 | "node": false,
9 | "sub": true,
10 | "undef": true,
11 | "devel": true,
12 | "unused": "vars",
13 | "boss": true,
14 | "eqnull": true,
15 | "browser": true,
16 | "globals": {
17 | "define": false,
18 | "require": false,
19 | "module": false
20 | },
21 | "globalstrict": true
22 | }
23 |
--------------------------------------------------------------------------------
/tasks/markdox.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('markdox', {
9 | dist: {
10 | files: [
11 | { src: 'src/events.js', dest: 'docs/api.md' }
12 | ]
13 | }
14 | });
15 | };
16 |
--------------------------------------------------------------------------------
/tasks/uglify.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('uglify', {
9 | options: {
10 | sourceMap: true
11 | },
12 | dist: {
13 | files: {
14 | 'build/events.min.js': ['build/events.js']
15 | }
16 | }
17 | });
18 | };
19 |
--------------------------------------------------------------------------------
/tasks/watch.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('watch', {
9 | dev: {
10 | options: {
11 | atBegin: true
12 | },
13 | files: ['Gruntfile.js', 'src/**/*.js', 'tests/**/*.js'],
14 | tasks: ['build', 'mocha']
15 | }
16 | });
17 | };
18 |
--------------------------------------------------------------------------------
/tasks/jscs.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('jscs', {
9 | src: [
10 | '**/*.js',
11 | '!node_modules/**',
12 | '!build/**',
13 | '!docs/**',
14 | '!tests/**'
15 | ],
16 | options: {
17 | config: '.jscsrc'
18 | }
19 | });
20 | };
21 |
--------------------------------------------------------------------------------
/tasks/mocha.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('mocha', {
9 | test: {
10 | src: ['tests/index.html'],
11 | options: {
12 | run: true,
13 | log: true,
14 | logErrors: true,
15 | timeout: 20000
16 | }
17 | }
18 | });
19 | };
20 |
--------------------------------------------------------------------------------
/tasks/bump.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('bump', {
9 | options: {
10 | files: ["package.json", "bower.json"],
11 | push: false,
12 | createTag: false,
13 | commitFiles: ['-a'],
14 | commitMessage: 'Start of release %VERSION%'
15 | }
16 | });
17 | };
18 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "events",
3 | "version": "0.0.1",
4 | "description": "A Backbone-like events system",
5 | "author": "Shane Tomlinson ",
6 | "main": "events.js",
7 | "moduleType": [
8 | "amd",
9 | "globals",
10 | "node"
11 | ],
12 | "keywords": [
13 | "DOM",
14 | "jQuery",
15 | "mircolibrary",
16 | "DOM",
17 | "manipulation"
18 | ],
19 | "license": "MPL 2.0",
20 | "ignore": [
21 | "**/.*",
22 | "node_modules",
23 | "bower_components",
24 | "test",
25 | "tests"
26 | ]
27 | }
28 |
--------------------------------------------------------------------------------
/tests/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "events-tests",
3 | "version": "0.0.0",
4 | "authors": [
5 | "Shane Tomlinson "
6 | ],
7 | "description": "The tests for events library",
8 | "moduleType": [
9 | "amd",
10 | "es6"
11 | ],
12 | "license": "MPL 2.0",
13 | "private": true,
14 | "ignore": [
15 | "**/.*",
16 | "node_modules",
17 | "bower_components",
18 | "test",
19 | "tests"
20 | ],
21 | "dependencies": {
22 | "chai": "1.10.0",
23 | "mocha": "2.1.0",
24 | "sinon": "http://sinonjs.org/releases/sinon-1.12.2.js"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/tasks/jshint.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('jshint', {
9 | config: {
10 | options: {jshintrc: '.jshintrc'},
11 | src: ['Gruntfile.js', 'tasks/*.js', 'config/**/*.js', 'node/**/*.js']
12 | },
13 | app: {
14 | options: {jshintrc: 'src/.jshintrc'},
15 | src: ['src/*.js', 'src/lib/**/*']
16 | }
17 | });
18 | };
19 |
--------------------------------------------------------------------------------
/tasks/copyright.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('copyright', {
9 | app: {
10 | options: {
11 | pattern: /This Source Code Form is subject to the terms of the Mozilla Public/
12 | },
13 | src: [
14 | '**/*.js',
15 | '!tests/bower_components/**',
16 | '!build/**',
17 | '!node_modules/**',
18 | '!docs/**'
19 | ]
20 | }
21 | });
22 | };
23 |
--------------------------------------------------------------------------------
/tests/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
14 |
15 |
16 |
17 |
18 |
19 |
20 | Events Unit Tests
21 |
22 |
23 |
24 |
25 |
26 |
27 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/tasks/buildcontrol.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | 'use strict';
7 |
8 | grunt.config('buildcontrol', {
9 | options: {
10 | commit: true,
11 | push: true,
12 | remote: 'git@github.com:shane-tomlinson/events.git'
13 | },
14 | release: {
15 | options: {
16 | branch: 'release',
17 | dir: 'build',
18 | tag: '<%= pkg.version %>'
19 | }
20 | },
21 | docs: {
22 | options: {
23 | branch: 'gh-pages',
24 | dir: 'docs',
25 | tag: 'docs-<%= pkg.version %>'
26 | }
27 | }
28 | });
29 | };
30 |
--------------------------------------------------------------------------------
/.jscsrc:
--------------------------------------------------------------------------------
1 | {
2 | "disallowKeywords": ["with", "eval"],
3 | "disallowKeywordsOnNewLine": ["else"],
4 | "requireSpaceBeforeBinaryOperators": ["?", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
5 | "disallowMultipleLineStrings": true,
6 | "requireSpaceAfterBinaryOperators": ["?", "/", "*", ":", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
7 | "disallowSpaceAfterObjectKeys": true,
8 | "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-"],
9 | "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
10 | "maximumLineLength": 420,
11 | "requireCapitalizedConstructors": true,
12 | "requireCurlyBraces": ["if", "else", "for", "while", "do"],
13 | "requireLineFeedAtFileEnd": true,
14 | "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return"],
15 | "validateIndentation": 2,
16 | "validateLineBreaks": "LF",
17 | "validateQuoteMarks": true,
18 | "validateJSDoc": {
19 | "checkParamNames": true,
20 | "checkRedundantParams": true,
21 | "requireParamTypes": true
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Gruntfile.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 | module.exports = function (grunt) {
6 | // load all grunt tasks matching the `grunt-*` pattern
7 | require('load-grunt-tasks')(grunt);
8 |
9 | var pkg = grunt.file.readJSON('package.json');
10 |
11 | grunt.initConfig({
12 | pkg: pkg,
13 | pkgReadOnly: pkg
14 | });
15 |
16 | // load local Grunt tasks
17 | grunt.loadTasks('tasks');
18 |
19 | grunt.registerTask('build',
20 | 'Build compressed resources',
21 | ['clean', 'lint', 'copy', 'uglify', 'bytesize']);
22 |
23 | grunt.registerTask('test',
24 | 'Run tests',
25 | ['mocha']);
26 |
27 | grunt.registerTask('lint',
28 | 'Alias for jshint and jscs tasks',
29 | ['jshint', 'jscs']);
30 |
31 | grunt.registerTask('default',
32 | ['build']);
33 |
34 | grunt.registerTask('doc',
35 | ['markdox']);
36 |
37 | grunt.registerTask('release',
38 | ['build', 'bump-only', 'changelog', 'bump-commit', 'doc', 'buildcontrol']);
39 | };
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Events
2 |
3 | A Backbone inspired events system.
4 |
5 | ## API:
6 | ### on(eventName, callback)
7 |
8 | Add an event listener
9 |
10 | #### Params:
11 |
12 | * **string** *eventName*
13 | * **function** *callback*
14 |
15 | ### off(eventName, callback)
16 |
17 | Remove an event listener
18 |
19 | #### Params:
20 |
21 | * **string** *eventName*
22 | * **function** *callback*
23 |
24 | ### trigger(eventName, [arguments])
25 |
26 | Trigger one or more event listeners.
27 |
28 | #### Params:
29 |
30 | * **string** *eventName*
31 | * **variant** *[arguments]*
32 |
33 | , args...
34 |
35 | ### once(eventName, callback)
36 |
37 | Add an event listener that can be run at most one time.
38 |
39 | #### Params:
40 |
41 | * **string** *eventName*
42 | * **function** *callback*
43 |
44 | ## Author:
45 | * Shane Tomlinson
46 | * shane@shanetomlinson.com
47 | * stomlinson@mozilla.com
48 | * set117@yahoo.com
49 | * https://shanetomlinson.com
50 | * https://github.com/shane-tomlinson
51 | * @shane_tomlinson
52 |
53 | ## Get involved:
54 |
55 | ## License:
56 | This software is available under version 2.0 of the MPL:
57 |
58 | https://www.mozilla.org/MPL/
59 |
60 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "events",
3 | "version": "0.0.1",
4 | "description": "A Backbone-like events system",
5 | "main": "index.js",
6 | "scripts": {
7 | "start": "grunt",
8 | "test": "grunt test",
9 | "setup": "npm install && npm run-script setup-bower",
10 | "setup-bower": "cd tests && bower install && cd ..",
11 | "contributors": "git shortlog -s | cut -c8- | sort -f > CONTRIBUTORS.md"
12 | },
13 | "directories": {
14 | "test": "tests"
15 | },
16 | "author": "Shane Tomlinson ",
17 | "license": "MPL 2.0",
18 | "devDependencies": {
19 | "bower": "1.3.12",
20 | "grunt": "0.4.5",
21 | "grunt-build-control": "git://github.com/robwierzbowski/grunt-build-control#274952",
22 | "grunt-bump": "0.0.16",
23 | "grunt-bytesize": "0.1.1",
24 | "grunt-cli": "0.1.13",
25 | "grunt-contrib-clean": "0.6.0",
26 | "grunt-contrib-copy": "0.7.0",
27 | "grunt-contrib-jshint": "0.10.0",
28 | "grunt-contrib-uglify": "0.7.0",
29 | "grunt-contrib-watch": "0.6.1",
30 | "grunt-conventional-changelog": "1.1.0",
31 | "grunt-copyright": "0.1.0",
32 | "grunt-jscs": "1.1.0",
33 | "grunt-markdox": "1.2.1",
34 | "grunt-mocha": "0.4.11",
35 | "load-grunt-tasks": "2.0.0"
36 | },
37 | "dependencies": {}
38 | }
39 |
--------------------------------------------------------------------------------
/tests/spec/events.js:
--------------------------------------------------------------------------------
1 | /* This Source Code Form is subject to the terms of the Mozilla Public
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 |
5 |
6 | describe('events', function () {
7 | 'use strict';
8 |
9 | var assert = chai.assert;
10 |
11 | var testObj;
12 |
13 | beforeEach(function () {
14 | testObj = Object.create(Events);
15 | });
16 |
17 | describe('on/trigger/off', function () {
18 | it('on attaches an event handler, trigger triggers an event, off stops listening', function () {
19 | var spy = sinon.spy();
20 |
21 | testObj.on('listen-for', spy);
22 | testObj.trigger('listen-for', 1, 2, 3);
23 | testObj.trigger('listen-for', 4, 5, 6);
24 | testObj.off('listen-for', spy);
25 | testObj.trigger('listen-for', 7, 8, 9);
26 |
27 | assert.isTrue(spy.calledTwice);
28 | assert.isTrue(spy.calledWith(1, 2, 3));
29 | assert.isTrue(spy.calledWith(4, 5, 6));
30 | assert.isFalse(spy.calledWith(7, 8, 9));
31 | });
32 | });
33 |
34 | describe('once/trigger', function () {
35 | it('only triggers an event once', function () {
36 | var spy = sinon.spy();
37 |
38 | testObj.once('listen-for', spy);
39 | testObj.trigger('listen-for', 1, 2, 3);
40 | testObj.trigger('listen-for', 4, 5, 6);
41 |
42 | assert.isTrue(spy.calledOnce);
43 | assert.isTrue(spy.calledWith(1, 2, 3));
44 | assert.isFalse(spy.calledWith(4, 5, 6));
45 | });
46 |
47 | it('can be removed before being triggered', function () {
48 | var spy = sinon.spy();
49 |
50 | testObj.once('listen-for', spy);
51 | testObj.off('listen-for', spy);
52 |
53 | testObj.trigger('listen-for', 1, 2, 3);
54 |
55 | assert.isFalse(spy.called);
56 | });
57 | });
58 | });
59 |
60 |
--------------------------------------------------------------------------------
/src/events.js:
--------------------------------------------------------------------------------
1 | // This Source Code Form is subject to the terms of the Mozilla Public
2 | // License, v. 2.0. If a copy of the MPL was not distributed with this
3 | // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 |
5 | /**
6 | * @class Events
7 | * @author Shane Tomlinson shane@shanetomlinson.com
8 | * @version 0.0.0
9 | */
10 |
11 | // jscs: disable
12 | // jshint ignore:start
13 | ;(function(define){define(function(require,exports,module){
14 | // jshint ignore:end
15 | // jscs: enable
16 | 'use strict';
17 |
18 | function hasHandler(handlers, eventName) {
19 | return eventName in handlers;
20 | }
21 |
22 | module.exports = {
23 | /**
24 | * Add an event listener
25 | * @method on
26 | * @param {string} eventName
27 | * @param {function} callback
28 | */
29 | on: function (eventName, callback) {
30 | if (! this._handlers) {
31 | this._handlers = {};
32 | }
33 |
34 | this._handlers[eventName] = this._handlers[eventName] || [];
35 | this._handlers[eventName].push(callback);
36 | },
37 |
38 | /**
39 | * Remove an event listener
40 | * @method off
41 | * @param {string} eventName
42 | * @param {function} callback
43 | */
44 | off: function (eventName, callback) {
45 | if (! hasHandler(this._handlers, eventName)) {
46 | return;
47 | }
48 |
49 | this._handlers[eventName] = this._handlers[eventName].filter(function(handler) {
50 | return (! (handler === callback || handler.__wraps === callback));
51 | });
52 | },
53 |
54 | /**
55 | * Trigger one or more event listeners.
56 | * @method trigger
57 | * @param {string} eventName
58 | */
59 | trigger: function (eventName/*, args...*/) {
60 | if (! hasHandler(this._handlers, eventName)) {
61 | return;
62 | }
63 |
64 | var args = [].slice.call(arguments, 1);
65 | this._handlers[eventName].forEach(function(func) {
66 | func.apply(null, args);
67 | });
68 | },
69 |
70 | /**
71 | * Add an event listener that can be run at most one time.
72 | * @method once
73 | * @param {string} eventName
74 | * @param {function} callback
75 | */
76 | once: function (eventName, callback) {
77 | var self = this;
78 | var handler = function () {
79 | self.off(eventName, handler);
80 | var args = [].slice.call(arguments, 0);
81 | callback.apply(null, args);
82 | };
83 | // used to remove the handler before being triggered.
84 | handler.__wraps = callback;
85 |
86 | self.on(eventName, handler);
87 | }
88 | };
89 |
90 | // jscs: disable
91 | // jshint ignore:start
92 | });})(typeof define=='function'&&define.amd?define
93 | :(function(n,w){'use strict';return typeof module=='object'?function(c){
94 | c(require,exports,module);}:function(c){var m={exports:{}};c(function(n){
95 | return w[n];},m.exports,m);w[n]=m.exports;};})('Events',this));
96 | // jshint ignore:end
97 | // jscs: enable
98 |
99 |
--------------------------------------------------------------------------------