Skip to content

Commit

Permalink
Merge pull request #123 from webcreate/history-extension
Browse files Browse the repository at this point in the history
History extension
  • Loading branch information
fieg committed Feb 23, 2014
2 parents 98914b1 + bc4cc49 commit 1a43844
Show file tree
Hide file tree
Showing 16 changed files with 700 additions and 158 deletions.
5 changes: 5 additions & 0 deletions .scrutinizer.yml
@@ -0,0 +1,5 @@
tools:
js_hint:
enabled: true
filter:
paths: ['src/']
10 changes: 10 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,16 @@
Changelog
=========

## 2.x

* Added History extension
* Added `ready` event
* Added `loaded` event (`load` is now triggered before loading starts)
* Added `rendered` event (`render` is now triggered before rendering starts)
* Added priority to callbacks
* Added `initialize` call for extensions
* Added `one` method

## 2.0.0

* Completely rewritten
Expand Down
2 changes: 1 addition & 1 deletion Gruntfile.js
Expand Up @@ -34,7 +34,7 @@ module.exports = function(grunt) {
separator: ';'
},
dist: {
src: ['<banner:meta.banner>', 'src/callbacks.js', 'src/extension/*.js', 'src/jquery-ias.js'],
src: ['<banner:meta.banner>', 'src/callbacks.js', 'src/jquery-ias.js', 'src/extension/*.js'],
dest: 'dist/<%= pkg.name %>.js'
}
},
Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
@@ -1,3 +1,9 @@
Upgrade from 2.0 to 2.x
=======================

* `render` event should be replaced with `rendered`
* `load` event should be replaced with `loaded`

Upgrade from 1.x to 2.0
=======================

Expand Down
139 changes: 98 additions & 41 deletions src/callbacks.js
Expand Up @@ -11,86 +11,131 @@
* Copyright 2014 Webcreate (Jeroen Fiege)
*/

var IASCallbacks = function() {
var IASCallbacks = function () {
this.list = [];
this.fireStack = [];
this.isFiring = false;
this.isDisabled = false;

/**
* Adds a callback
* Calls all added callbacks
*
* @param callback
* @returns {IASCallbacks}
* @private
* @param args
*/
this.add = function(callback) {
this.list.push(callback);
this.fire = function (args) {
var context = args[0],
deferred = args[1],
callbackArguments = args[2];
this.isFiring = true;

return this;
for (var i = 0, l = this.list.length; i < l; i++) {
if (false === this.list[i].fn.apply(context, callbackArguments)) {
deferred.reject();

break;
}
}

this.isFiring = false;

deferred.resolve();

if (this.fireStack.length) {
this.fire(this.fireStack.shift());
}
};

/**
* Removes a callback
* Returns index of the callback in the list in a similar way as
* the indexOf function.
*
* @param callback
* @returns {IASCallbacks}
* @param {number} index index to start the search from
* @returns {number}
*/
this.remove = function(callback) {
var index;
this.inList = function (callback, index) {
index = index || 0;

while( ( index = jQuery.inArray( callback, this.list, index ) ) > -1 ) {
this.list.splice( index, 1 );
for (var i = index, length = this.list.length; i < length; i++) {
if (this.list[i].fn === callback || (callback.guid && this.list[i].fn.guid && callback.guid === this.list[i].fn.guid)) {
return i;
}
}

return this;
return -1;
};

return this;
};

IASCallbacks.prototype = {
/**
* Checks if callback is added
* Adds a callback
*
* @param callback
* @returns {*}
* @returns {IASCallbacks}
* @param priority
*/
this.has = function(callback) {
return jQuery.inArray(callback, this.list);
};
add: function (callback, priority) {
var callbackObject = {fn: callback, priority: priority};

priority = priority || 0;

for (var i = 0, length = this.list.length; i < length; i++) {
if (priority > this.list[i].priority) {
this.list.splice(i, 0, callbackObject);

return this;
}
}

this.list.push(callbackObject);

return this;
},

/**
* Calls all added callbacks
* Removes a callback
*
* @param args
* @param callback
* @returns {IASCallbacks}
*/
this.fire = function(args) {
var context = args[0],
deferred = args[1],
callbackArguments = args[2];
this.isFiring = true;
remove: function (callback) {
var index = 0;

for (var i = 0, l = this.list.length; i < l; i++) {
if (false === this.list[i].apply(context, callbackArguments)) {
deferred.reject();
break;
}
while (( index = this.inList(callback, index) ) > -1) {
this.list.splice(index, 1);
}

this.isFiring = false;
return this;
},

deferred.resolve();
/**
* Checks if callback is added
*
* @param callback
* @returns {*}
*/
has: function (callback) {
return (this.inList(callback) > -1);
},

if (this.fireStack.length) {
this.fire(this.fireStack.shift());
}
};

/**
* Calls callbacks with a context
*
* @param context
* @param args
* @returns {IASCallbacks}
* @returns {object|void}
*/
this.fireWith = function(context, args) {
fireWith: function (context, args) {
var deferred = $.Deferred();

if (this.isDisabled) {
return deferred.reject();
}

args = args || [];
args = [ context, deferred, args.slice ? args.slice() : args ];

Expand All @@ -101,7 +146,19 @@ var IASCallbacks = function() {
}

return deferred;
};
},

return this;
/**
* Disable firing of new events
*/
disable: function () {
this.isDisabled = true;
},

/**
* Enable firing of new events
*/
enable: function () {
this.isDisabled = false;
}
};

0 comments on commit 1a43844

Please sign in to comment.