Skip to content

Commit

Permalink
feat: make listeners chainable and return transition result in navigate
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jul 6, 2015
1 parent b7e36b2 commit f725296
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "router5",
"version": "0.1.0-rc.2",
"version": "0.1.0-rc.3",
"homepage": "http://router5.github.io",
"authors": [
"Thomas Roch <thomas.c.roch@gmail.com>"
Expand Down
28 changes: 20 additions & 8 deletions dist/browser/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,15 @@ var Router5 = (function () {
value: function registerComponent(name, component) {
if (this.activeComponents[name]) console.warn('A component was alread registered for route node ' + name + '.');
this.activeComponents[name] = component;
return this;
}
}, {
key: 'deregisterComponent',

/**
* Deregister an active component
* @param {String} name The route segment full name
* @return {Router5} The router instance
*/
value: function deregisterComponent(name) {
delete this.activeComponents[name];
Expand All @@ -704,6 +706,7 @@ var Router5 = (function () {
}
if (!this.callbacks[name]) this.callbacks[name] = [];
this.callbacks[name].push(cb);
return this;
}
}, {
key: '_removeListener',
Expand All @@ -712,30 +715,32 @@ var Router5 = (function () {
* @private
*/
value: function _removeListener(name, cb) {
if (!this.callbacks[name]) return;
this.callbacks[name] = this.callbacks[name].filter(function (callback) {
if (this.callbacks[name]) this.callbacks[name] = this.callbacks[name].filter(function (callback) {
return callback !== cb;
});
return this;
}
}, {
key: 'addListener',

/**
* Add a route change listener
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addListener(cb) {
this._addListener('', cb);
return this._addListener('', cb);
}
}, {
key: 'removeListener',

/**
* Remove a route change listener
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeListener(cb) {
this._removeListener('', cb);
return this._removeListener('', cb);
}
}, {
key: 'addNodeListener',
Expand All @@ -744,9 +749,10 @@ var Router5 = (function () {
* Add a node change listener
* @param {String} name The route segment full name
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addNodeListener(name, cb) {
this._addListener('^' + name, cb);
return this._addListener('^' + name, cb);
}
}, {
key: 'removeNodeListener',
Expand All @@ -755,9 +761,10 @@ var Router5 = (function () {
* Remove a node change listener
* @param {String} name The route segment full name
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeNodeListener(name, cb) {
this._removeListener('^' + name, cb);
return this._removeListener('^' + name, cb);
}
}, {
key: 'addRouteListener',
Expand All @@ -766,9 +773,10 @@ var Router5 = (function () {
* Add a route change listener
* @param {String} name The route name to listen to
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addRouteListener(name, cb) {
this._addListener('=' + name, cb);
return this._addListener('=' + name, cb);
}
}, {
key: 'removeRouteListener',
Expand All @@ -777,9 +785,10 @@ var Router5 = (function () {
* Remove a route change listener
* @param {String} name The route name to listen to
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeRouteListener(name, cb) {
this._removeListener('=' + name, cb);
return this._removeListener('=' + name, cb);
}
}, {
key: 'buildPath',
Expand Down Expand Up @@ -813,6 +822,7 @@ var Router5 = (function () {
* @param {String} name The route name
* @param {Object} params The route params
* @param {Object} opts The route options (replace, reload)
* @return {Boolean} Whether or not transition was allowed
*/
value: function navigate(name) {
var params = arguments[1] === undefined ? {} : arguments[1];
Expand All @@ -837,6 +847,8 @@ var Router5 = (function () {
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}

return canTransition;
}
}]);

Expand Down
2 changes: 1 addition & 1 deletion dist/browser/router5.min.js

Large diffs are not rendered by default.

28 changes: 20 additions & 8 deletions dist/commonjs/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,15 @@ var Router5 = (function () {
value: function registerComponent(name, component) {
if (this.activeComponents[name]) console.warn('A component was alread registered for route node ' + name + '.');
this.activeComponents[name] = component;
return this;
}
}, {
key: 'deregisterComponent',

/**
* Deregister an active component
* @param {String} name The route segment full name
* @return {Router5} The router instance
*/
value: function deregisterComponent(name) {
delete this.activeComponents[name];
Expand All @@ -265,6 +267,7 @@ var Router5 = (function () {
}
if (!this.callbacks[name]) this.callbacks[name] = [];
this.callbacks[name].push(cb);
return this;
}
}, {
key: '_removeListener',
Expand All @@ -273,30 +276,32 @@ var Router5 = (function () {
* @private
*/
value: function _removeListener(name, cb) {
if (!this.callbacks[name]) return;
this.callbacks[name] = this.callbacks[name].filter(function (callback) {
if (this.callbacks[name]) this.callbacks[name] = this.callbacks[name].filter(function (callback) {
return callback !== cb;
});
return this;
}
}, {
key: 'addListener',

/**
* Add a route change listener
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addListener(cb) {
this._addListener('', cb);
return this._addListener('', cb);
}
}, {
key: 'removeListener',

/**
* Remove a route change listener
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeListener(cb) {
this._removeListener('', cb);
return this._removeListener('', cb);
}
}, {
key: 'addNodeListener',
Expand All @@ -305,9 +310,10 @@ var Router5 = (function () {
* Add a node change listener
* @param {String} name The route segment full name
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addNodeListener(name, cb) {
this._addListener('^' + name, cb);
return this._addListener('^' + name, cb);
}
}, {
key: 'removeNodeListener',
Expand All @@ -316,9 +322,10 @@ var Router5 = (function () {
* Remove a node change listener
* @param {String} name The route segment full name
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeNodeListener(name, cb) {
this._removeListener('^' + name, cb);
return this._removeListener('^' + name, cb);
}
}, {
key: 'addRouteListener',
Expand All @@ -327,9 +334,10 @@ var Router5 = (function () {
* Add a route change listener
* @param {String} name The route name to listen to
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addRouteListener(name, cb) {
this._addListener('=' + name, cb);
return this._addListener('=' + name, cb);
}
}, {
key: 'removeRouteListener',
Expand All @@ -338,9 +346,10 @@ var Router5 = (function () {
* Remove a route change listener
* @param {String} name The route name to listen to
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeRouteListener(name, cb) {
this._removeListener('=' + name, cb);
return this._removeListener('=' + name, cb);
}
}, {
key: 'buildPath',
Expand Down Expand Up @@ -374,6 +383,7 @@ var Router5 = (function () {
* @param {String} name The route name
* @param {Object} params The route params
* @param {Object} opts The route options (replace, reload)
* @return {Boolean} Whether or not transition was allowed
*/
value: function navigate(name) {
var params = arguments[1] === undefined ? {} : arguments[1];
Expand All @@ -398,6 +408,8 @@ var Router5 = (function () {
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}

return canTransition;
}
}]);

Expand Down
28 changes: 20 additions & 8 deletions dist/umd/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,15 @@
value: function registerComponent(name, component) {
if (this.activeComponents[name]) console.warn('A component was alread registered for route node ' + name + '.');
this.activeComponents[name] = component;
return this;
}
}, {
key: 'deregisterComponent',

/**
* Deregister an active component
* @param {String} name The route segment full name
* @return {Router5} The router instance
*/
value: function deregisterComponent(name) {
delete this.activeComponents[name];
Expand All @@ -272,6 +274,7 @@
}
if (!this.callbacks[name]) this.callbacks[name] = [];
this.callbacks[name].push(cb);
return this;
}
}, {
key: '_removeListener',
Expand All @@ -280,30 +283,32 @@
* @private
*/
value: function _removeListener(name, cb) {
if (!this.callbacks[name]) return;
this.callbacks[name] = this.callbacks[name].filter(function (callback) {
if (this.callbacks[name]) this.callbacks[name] = this.callbacks[name].filter(function (callback) {
return callback !== cb;
});
return this;
}
}, {
key: 'addListener',

/**
* Add a route change listener
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addListener(cb) {
this._addListener('', cb);
return this._addListener('', cb);
}
}, {
key: 'removeListener',

/**
* Remove a route change listener
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeListener(cb) {
this._removeListener('', cb);
return this._removeListener('', cb);
}
}, {
key: 'addNodeListener',
Expand All @@ -312,9 +317,10 @@
* Add a node change listener
* @param {String} name The route segment full name
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addNodeListener(name, cb) {
this._addListener('^' + name, cb);
return this._addListener('^' + name, cb);
}
}, {
key: 'removeNodeListener',
Expand All @@ -323,9 +329,10 @@
* Remove a node change listener
* @param {String} name The route segment full name
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeNodeListener(name, cb) {
this._removeListener('^' + name, cb);
return this._removeListener('^' + name, cb);
}
}, {
key: 'addRouteListener',
Expand All @@ -334,9 +341,10 @@
* Add a route change listener
* @param {String} name The route name to listen to
* @param {Function} cb The listener to add
* @return {Router5} The router instance
*/
value: function addRouteListener(name, cb) {
this._addListener('=' + name, cb);
return this._addListener('=' + name, cb);
}
}, {
key: 'removeRouteListener',
Expand All @@ -345,9 +353,10 @@
* Remove a route change listener
* @param {String} name The route name to listen to
* @param {Function} cb The listener to remove
* @return {Router5} The router instance
*/
value: function removeRouteListener(name, cb) {
this._removeListener('=' + name, cb);
return this._removeListener('=' + name, cb);
}
}, {
key: 'buildPath',
Expand Down Expand Up @@ -381,6 +390,7 @@
* @param {String} name The route name
* @param {Object} params The route params
* @param {Object} opts The route options (replace, reload)
* @return {Boolean} Whether or not transition was allowed
*/
value: function navigate(name) {
var params = arguments[1] === undefined ? {} : arguments[1];
Expand All @@ -405,6 +415,8 @@
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}

return canTransition;
}
}]);

Expand Down

0 comments on commit f725296

Please sign in to comment.