Skip to content

Commit

Permalink
return a promise as a route change callback
Browse files Browse the repository at this point in the history
allows for post route change logic when modifying the route
programatically.
  • Loading branch information
Stephen von Takach committed Nov 17, 2013
1 parent f1d1a8f commit e48bff8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "ngRouteNames",
"version": "1.1.0",
"version": "1.1.1",
"main": "./ngRouteNames.js",
"dependencies": {
"angular": "~1.2.0",
"angular": "~1.2.1",
"angular-gesture": "~1.3.0"
}
}
36 changes: 34 additions & 2 deletions ngRouteNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

.provider('$routeNames', ['$routeProvider', function ($routeProvider) {
var namedRoutes = {},
paramMatcher = /(?:\:)(.*?)(?:(\/|$)\/?)/gm;
paramMatcher = /(?:\:)(.*?)(?:(\/|$)\/?)/gm,
pendingRoutes = [];

this.when = function (path, route) {
if (route.name) {
Expand Down Expand Up @@ -35,7 +36,7 @@
this.otherwise = $routeProvider.otherwise;
this.activeClass = 'is-active';

this.$get = ['$interpolate', '$route', '$routeParams', '$location', function ($interpolate, $route, $routeParams, $location) {
this.$get = ['$interpolate', '$route', '$routeParams', '$location', '$q', '$rootScope', function ($interpolate, $route, $routeParams, $location, $q, $rootScope) {
var result,
checkSearch = function (searchParams, asString) {
// Are search params involved?
Expand Down Expand Up @@ -70,6 +71,27 @@
return '';
};

// Resolve route changes
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
var name = $route.current.name,
params = arguments,
i;

if (name === undefined) {
return;
}

for (i = 0; i < pendingRoutes.length; i++) {
if (pendingRoutes[i][0] === name) {
pendingRoutes[i][1].resolve(params);
} else {
pendingRoutes[i][1].reject(params);
}
}

pendingRoutes = [];
});

// Create an interpolation function for the routes
angular.forEach(namedRoutes, function (route, name) {
result = $interpolate(route, true);
Expand All @@ -88,9 +110,14 @@
if (typeof route === 'function') {
// For routes where interpolation is required
$route.to[name] = function (params, search) {
var defer = $q.defer();

params = angular.extend({}, $routeParams, params);
$location.path(route(params));
checkSearch(search, false);

pendingRoutes.push([name, defer]);
return defer.promise;
};
$route.pathTo[name] = function (params, search) {
params = angular.extend({}, $routeParams, params);
Expand All @@ -99,8 +126,13 @@
} else {
// Less processing for static routes
$route.to[name] = function (params, search) {
var defer = $q.defer();

$location.path(route);
checkSearch(search, false);

pendingRoutes.push([name, defer]);
return defer.promise;
};
$route.pathTo[name] = function (params, search) {
return [route, checkSearch(search, true)];
Expand Down

0 comments on commit e48bff8

Please sign in to comment.