Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit edca07b

Browse files
committed
use different fix to sustain order for async templates
1 parent f9104d1 commit edca07b

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

src/ngMessages/messages.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ angular.module('ngMessages', [])
415415
forEach(messages, function(item) {
416416
item.message.detach();
417417
});
418-
})
418+
});
419419

420420
this.reRender = function() {
421421
if (!renderLater) {
@@ -448,19 +448,22 @@ angular.module('ngMessages', [])
448448
ctrl.reRender();
449449
};
450450

451-
function findPreviousMessage(parent, comment, key) {
451+
function findPreviousMessage(parent, comment) {
452452
var prevNode = comment;
453453
var parentLookup = [];
454454
while (prevNode && prevNode !== parent) {
455455
var prevKey = prevNode.$$ngMessageNode;
456-
if (prevKey && prevKey.length && prevKey < key) {
456+
if (prevKey && prevKey.length) {
457457
// Only add elements whose key is actually lower than the starting element's
458458
return messages[prevKey];
459459
}
460460

461461
// dive deeper into the DOM and examine its children for any ngMessage
462462
// comments that may be in an element that appears deeper in the list
463-
if (prevNode.childNodes.length && parentLookup.indexOf(prevNode) == -1) {
463+
if (prevNode.childNodes.length &&
464+
prevNode !== comment.parentNode &&
465+
parentLookup.indexOf(prevNode) == -1
466+
) {
464467
parentLookup.push(prevNode);
465468
prevNode = prevNode.childNodes[prevNode.childNodes.length - 1];
466469
} else {
@@ -474,7 +477,7 @@ angular.module('ngMessages', [])
474477
if (!ctrl.head) {
475478
ctrl.head = messageNode;
476479
} else {
477-
var match = findPreviousMessage(parent, comment, key);
480+
var match = findPreviousMessage(parent, comment);
478481
if (match) {
479482
messageNode.next = match.next;
480483
match.next = messageNode;
@@ -488,7 +491,7 @@ angular.module('ngMessages', [])
488491
function removeMessageNode(parent, comment, key) {
489492
var messageNode = messages[key];
490493

491-
var match = findPreviousMessage(parent, comment, key);
494+
var match = findPreviousMessage(parent, comment);
492495
if (match) {
493496
match.next = messageNode.next;
494497
} else {

test/ngMessages/messagesSpec.js

+16-32
Original file line numberDiff line numberDiff line change
@@ -858,42 +858,26 @@ describe('ngMessages', function() {
858858
}));
859859
});
860860

861-
it('should sustain the order when messages are included by a template with an async template', function() {
862-
module(function($compileProvider) {
863-
$compileProvider.directive('inclusor', function() {
864-
return {
865-
templateUrl: 'include.html'
866-
};
867-
});
868-
});
869-
861+
it('should properly detect the previous message, even if it was registered later',
870862
inject(function($compile, $rootScope, $templateCache) {
871-
var html;
872-
$templateCache.put('include.html', '<div ng-message="a">A</div>' +
873-
'<div ng-message="b">B</div>');
874-
875-
html = '<div ng-messages-multiple ng-messages="items"><div inclusor>' +
876-
'</div><div ng-message="c">C</div><div ng-message="d">D</div></div>';
877-
$rootScope.items = {};
878-
879-
$rootScope.$apply(function() {
880-
$rootScope.items.b = true;
881-
$rootScope.items.d = true;
882-
});
863+
$templateCache.put('include.html', '<div ng-message="a">A</div>');
864+
var html =
865+
'<div ng-messages="items">' +
866+
'<div ng-include="\'include.html\'"></div>' +
867+
'<div ng-message="b">B</div>' +
868+
'<div ng-message="c">C</div>' +
869+
'</div>';
883870

884871
element = $compile(html)($rootScope);
885-
$rootScope.$digest();
886-
887-
expect(element.text().trim()).toBe('BD');
888-
889-
$rootScope.$apply(function() {
890-
$rootScope.items.a = true;
891-
$rootScope.items.c = true;
892-
});
872+
$rootScope.$apply('items = {b: true, c: true}');
893873

894-
expect(element.text().trim()).toBe('ABCD');
895-
});
874+
expect(element.text()).toBe('B');
896875

897-
});
876+
var nodeB = element[0].querySelector('[ng-message="b"]');
877+
angular.element(nodeB).remove();
878+
$rootScope.$apply('items.a = true');
898879

880+
expect(element.text()).toBe('A');
881+
})
882+
);
899883
});

0 commit comments

Comments
 (0)