Skip to content

Commit

Permalink
fix(ncyBreadcrumb): display the correct breadcrumb in case of direct …
Browse files Browse the repository at this point in the history
…access

When the breadcrumb relies on nested views, the $viewContentLoaded event used by the directive occurs many times. The event related to the deepest view (the correct one for the breadcrumb) is not necessary the last one.
We use the '$id' property to get the 'lastly created' scope.
  • Loading branch information
ncuillery committed May 1, 2014
1 parent ab2ae1a commit 97cc0ce
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 20 deletions.
31 changes: 22 additions & 9 deletions dist/angular-breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
* Copyright (c) 2014 Nicolas Cuillery; Licensed MIT */

(function (window, angular, undefined) {
function isAOlderThanB(scopeA, scopeB) {
if(angular.equals(scopeA.length, scopeB.length)) {
return scopeA > scopeB;
} else {
return scopeA.length > scopeB.length;
}
}

function $Breadcrumb() {

var $$options = {
Expand Down Expand Up @@ -149,16 +157,21 @@ function BreadcrumbDirective($interpolate, $breadcrumb, $rootScope) {
templateUrl: $breadcrumb.getTemplateUrl(),
link: {
post: function postLink(scope) {
var lastScopeId;
$rootScope.$on('$viewContentLoaded', function (event) {
scope.steps = $breadcrumb.getStatesChain();
angular.forEach(scope.steps, function (value) {
if (value.data && value.data.ncyBreadcrumbLabel) {
var parseLabel = $interpolate(value.data.ncyBreadcrumbLabel);
value.ncyBreadcrumbLabel = parseLabel(event.targetScope);
} else {
value.ncyBreadcrumbLabel = value.name;
}
});
// With nested views, the event occur several times, in "wrong" order
if(!lastScopeId || isAOlderThanB(event.targetScope.$id, lastScopeId)) {
lastScopeId = event.targetScope.$id;
scope.steps = $breadcrumb.getStatesChain();
angular.forEach(scope.steps, function (value) {
if (value.data && value.data.ncyBreadcrumbLabel) {
var parseLabel = $interpolate(value.data.ncyBreadcrumbLabel);
value.ncyBreadcrumbLabel = parseLabel(event.targetScope);
} else {
value.ncyBreadcrumbLabel = value.name;
}
});
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-breadcrumb.min.js

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

32 changes: 22 additions & 10 deletions src/angular-breadcrumb.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function isAOlderThanB(scopeA, scopeB) {
if(angular.equals(scopeA.length, scopeB.length)) {
return scopeA > scopeB;
} else {
return scopeA.length > scopeB.length;
}
}

function $Breadcrumb() {

var $$options = {
Expand Down Expand Up @@ -112,7 +120,6 @@ function $Breadcrumb() {
}
};
}];

}

function BreadcrumbDirective($interpolate, $breadcrumb, $rootScope) {
Expand Down Expand Up @@ -144,16 +151,21 @@ function BreadcrumbDirective($interpolate, $breadcrumb, $rootScope) {
templateUrl: $breadcrumb.getTemplateUrl(),
link: {
post: function postLink(scope) {
var lastScopeId;
$rootScope.$on('$viewContentLoaded', function (event) {
scope.steps = $breadcrumb.getStatesChain();
angular.forEach(scope.steps, function (value) {
if (value.data && value.data.ncyBreadcrumbLabel) {
var parseLabel = $interpolate(value.data.ncyBreadcrumbLabel);
value.ncyBreadcrumbLabel = parseLabel(event.targetScope);
} else {
value.ncyBreadcrumbLabel = value.name;
}
});
// With nested views, the event occur several times, in "wrong" order
if(!lastScopeId || isAOlderThanB(event.targetScope.$id, lastScopeId)) {
lastScopeId = event.targetScope.$id;
scope.steps = $breadcrumb.getStatesChain();
angular.forEach(scope.steps, function (value) {
if (value.data && value.data.ncyBreadcrumbLabel) {
var parseLabel = $interpolate(value.data.ncyBreadcrumbLabel);
value.ncyBreadcrumbLabel = parseLabel(event.targetScope);
} else {
value.ncyBreadcrumbLabel = value.name;
}
});
}
});
}
}
Expand Down
47 changes: 47 additions & 0 deletions test/spec/scope-compare-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*jshint undef: false */

describe('The scope', function() {

beforeEach(function() {
module('ncy-basic-conf');
});

it('00A is older than 001', function() {
expect(isAOlderThanB('00A', '001')).toBe(true);
});

it('010 is older than 00Y', function() {
expect(isAOlderThanB('010', '00Y')).toBe(true);
});

it('01P is older than 010', function() {
expect(isAOlderThanB('01P', '010')).toBe(true);
});

it('FOO is older than BAR', function() {
expect(isAOlderThanB('FOO', 'BAR')).toBe(true);
});

it('F00 is older than BAR', function() {
expect(isAOlderThanB('F00', 'BAR')).toBe(true);
});

it('0000 is older than ZZZ', function() {
expect(isAOlderThanB('0000', 'ZZZ')).toBe(true);
});

it('(newly created) is always older than the precedent one', inject(function($rootScope) {
var scope = $rootScope.$new();
for(var i = 0; i < 100000; i++) {
var newScope = $rootScope.$new();
var isOlder = isAOlderThanB(newScope.$id, scope.$id);
expect(isOlder).toBe(true);
if(!isOlder) {
console.log(newScope.$id, scope.$id, isOlder, i);
break;
}
scope = newScope;
}
}));

});

0 comments on commit 97cc0ce

Please sign in to comment.