Skip to content

Commit

Permalink
introduce new $routeSegment.$routeParams feature, various bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
artch committed Aug 20, 2013
1 parent 2054814 commit 89d94f1
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 59 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Documentation
Please note that you may read the [test specs](https://github.com/artch/angular-route-segment/tree/master/test/unit) to learn features usage.
### $routeSegmentProvider ###
### $routeSegmentProvider properties ###
**options**
Expand Down Expand Up @@ -190,12 +190,17 @@ Traverses up in the tree.
Traverses to the root.
### $routeSegment ###
### $routeSegment properties ###
**name**
Fully qualified name of current active route.
**$routeParams**
A copy of `$routeParams` in its state of the latest successful segment update. It may be not equal to `$routeParams`
while some resolving is not completed yet. Should be used instead of original `$routeParams` in most cases.
**chain**
An array of segments splitted by each level separately. Each item contains the following properties:
Expand Down
74 changes: 51 additions & 23 deletions build/angular-route-segment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* angular-route-segment v1.0.3
* angular-route-segment v1.1.0
* https://angular-route-segment.com
* @author Artem Chivchalov
* @license MIT License http://opensource.org/licenses/MIT
Expand Down Expand Up @@ -145,7 +145,15 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
* Fully qualified name of current active route
* @type {string}
*/
name: '',
name: '',

/**
* A copy of `$routeParams` in its state of the latest successful segment update. It may be not equal
* to `$routeParams` while some resolving is not completed yet. Should be used instead of original
* `$routeParams` in most cases.
* @type {Object}
*/
$routeParams: angular.copy($routeParams),

/**
* Array of segments splitted by each level separately. Each item contains the following properties:
Expand Down Expand Up @@ -180,8 +188,6 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
return false;
}
};

var lastParams = angular.copy($routeParams);

var resolvingSemaphoreChain = {};

Expand All @@ -200,27 +206,37 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
var newSegment = getSegmentInChain( i, segmentNameChain );

if(resolvingSemaphoreChain[i] != newSegment.name || isDependenciesChanged(newSegment)) {
/*if($routeSegment.chain[i] && $routeSegment.chain[i].name == newSegment.name &&

if($routeSegment.chain[i] && $routeSegment.chain[i].name == newSegment.name &&
!isDependenciesChanged(newSegment))
// if we went back to the same state as we were before resolving new segment
resolvingSemaphoreChain[i] = newSegment.name;
else */
else
updates.push(updateSegment(i, newSegment));
}
}

$q.all(updates).then(function() {
$q.all(updates).then(function(result) {

$routeSegment.name = segmentName;
$routeSegment.$routeParams = angular.copy($routeParams);

for(var i=0; i < result.length; i++) {
if(result[i].success != undefined)
broadcast(result[i].success);
}

// Removing redundant segment in case if new segment chain is shorter than old one
if($routeSegment.chain.length > segmentNameChain.length) {
for(var i=segmentNameChain.length; i < $routeSegment.chain.length; i++)
var oldLength = $routeSegment.chain.length;
var shortenBy = $routeSegment.chain.length - segmentNameChain.length;
$routeSegment.chain.splice(-shortenBy, shortenBy);
for(var i=segmentNameChain.length; i < oldLength; i++)
updateSegment(i, null);
$routeSegment.chain.splice(-1, $routeSegment.chain.length - segmentNameChain.length);
}
});

lastParams = angular.copy($routeParams);

}
});

Expand All @@ -229,7 +245,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
var result = false;
if(segment.params.dependencies)
angular.forEach(segment.params.dependencies, function(name) {
if(!angular.equals(lastParams[name], $routeParams[name]))
if(!angular.equals($routeSegment.$routeParams[name], $routeParams[name]))
result = true;
});
return result;
Expand All @@ -243,23 +259,25 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',

if(!segment) {
resolvingSemaphoreChain[index] = null;
$rootScope.$broadcast( 'routeSegmentChange', { index: index, segment: null } );
broadcast(index);
return;
}

resolvingSemaphoreChain[index] = segment.name;

if(segment.params.untilResolved) {
return resolveAndBroadcast(index, segment.name, segment.params.untilResolved)
.then(function() {
resolveAndBroadcast(index, segment.name, segment.params);
return resolve(index, segment.name, segment.params.untilResolved)
.then(function(result) {
if(result.success != undefined)
broadcast(index);
return resolve(index, segment.name, segment.params);
})
}
else
return resolveAndBroadcast(index, segment.name, segment.params);
return resolve(index, segment.name, segment.params);
}

function resolveAndBroadcast(index, name, params) {
function resolve(index, name, params) {

var locals = angular.extend({}, params.resolve);

Expand Down Expand Up @@ -289,7 +307,10 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
params: params,
locals: resolvedLocals,
reload: function() {
updateSegment(index, this);
updateSegment(index, this).then(function(result) {
if(result.success != undefined)
broadcast(index);
})
}
};

Expand All @@ -316,23 +337,27 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
$routeSegment.chain[index].reload();
})
}

$rootScope.$broadcast( 'routeSegmentChange', {
index: index,
segment: $routeSegment.chain[index] } );

return {success: index};
},

function(error) {

if(params.resolveFailed) {
var newResolve = {error: function() { return $q.when(error); }};
resolveAndBroadcast(index, name, angular.extend({resolve: newResolve}, params.resolveFailed));
return resolve(index, name, angular.extend({resolve: newResolve}, params.resolveFailed));
}
else
throw new Error('Resolving failed with a reason `'+error+'`, but no `resolveFailed` ' +
'provided for segment `'+name+'`');
})
}

function broadcast(index) {
$rootScope.$broadcast( 'routeSegmentChange', {
index: index,
segment: $routeSegment.chain[index] || null } );
}

function getSegmentInChain(segmentIdx, segmentNameChain) {

Expand Down Expand Up @@ -417,6 +442,9 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
}
catch(e) {}

if($routeSegment.chain[viewSegmentIndex])
update($routeSegment.chain[viewSegmentIndex]);

// Watching for the specified route segment and updating contents
$scope.$on('routeSegmentChange', function(event, args) {
if(args.index == viewSegmentIndex)
Expand Down
4 changes: 2 additions & 2 deletions build/angular-route-segment.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,22 @@ function MainCtrl($scope, $routeSegment, loader) {
})
}

function Section1Ctrl($scope, $routeSegment, $routeParams) {
function Section1Ctrl($scope, $routeSegment) {

$scope.$routeSegment = $routeSegment;
$scope.$routeParams = $routeParams;
$scope.test = { btnClicked: false };
$scope.items = [ 1,2,3,4,5 ];
}

function Section1ItemCtrl($scope, $routeSegment, $routeParams) {

$scope.$routeParams = $routeParams;
function Section1ItemCtrl($scope, $routeSegment) {

$scope.$routeSegment = $routeSegment;
$scope.item = { id: $routeParams.id };
$scope.item = { id: $routeSegment.$routeParams.id };
$scope.test = { textValue: '' };
}

function Section2Ctrl($scope, $routeSegment, $routeParams) {

$scope.$routeParams = $routeParams;
function Section2Ctrl($scope, $routeSegment) {

$scope.$routeSegment = $routeSegment;
$scope.test = { textValue: '' };
$scope.items = [ 1,2,3,4,5,6,7,8,9 ];
Expand Down
2 changes: 1 addition & 1 deletion example/templates/section1.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<li class="nav-header">Items</li>

<li ng-repeat="i in items"
ng-class="{active: $routeSegment.startsWith('s1.itemInfo') && $routeParams.id == i}">
ng-class="{active: $routeSegment.startsWith('s1.itemInfo') && $routeSegment.$routeParams.id == i}">

<a ng-href="#/section1/{{i}}">Item {{i}}</a>

Expand Down
2 changes: 1 addition & 1 deletion example/templates/section2.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h2>Section 2</h2>
<ul class="nav nav-pills">

<li ng-repeat="i in items"
ng-class="{active: $routeParams.id==i}">
ng-class="{active: $routeSegment.$routeParams.id==i}">

<a ng-href="#/section2/{{i}}">Item {{i}}</a>

Expand Down
2 changes: 1 addition & 1 deletion example/templates/section2/item.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Item {{$routeParams.id}} contents.
Item {{$routeSegment.$routeParams.id}} contents.
Loading

0 comments on commit 89d94f1

Please sign in to comment.