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

fix($animate): insert elements at the start of the parent container instead of at the end #6663

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/ng/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ var $AnimateProvider = ['$provide', function($provide) {
* @ngdoc method
* @name $animate#enter
* @function
* @description Inserts the element into the DOM either after the `after` element or within
* the `parent` element. Once complete, the done() callback will be fired (if provided).
* @description Inserts the element into the DOM either after the `after` element or
* as the first child within the `parent` element. Once complete, the done() callback
* will be fired (if provided).
* @param {DOMElement} element the element which will be inserted into the DOM
* @param {DOMElement} parent the parent element which will append the element as
* a child (if the after element is not present)
Expand All @@ -122,14 +123,9 @@ var $AnimateProvider = ['$provide', function($provide) {
* inserted into the DOM
*/
enter : function(element, parent, after, done) {
if (after) {
after.after(element);
} else {
if (!parent || !parent[0]) {
parent = after.parent();
}
parent.append(element);
}
after
? after.after(element)
: parent.prepend(element);
async(done);
},

Expand Down
13 changes: 13 additions & 0 deletions test/ng/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ describe("$animate", function() {
expect(element.contents().length).toBe(1);
}));

it("should enter the element to the start of the parent container",
inject(function($animate, $compile, $rootScope) {

for(var i = 0; i < 5; i++) {
element.append(jqLite('<div> ' + i + '</div>'));
}

var child = jqLite('<div>first</div>');
$animate.enter(child, element);

expect(element.text()).toEqual('first 0 1 2 3 4');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems a bit surprising to me that this is the default behavior.

I came across this PR by chance and when I looked at the diff I had a hard time understanding what you are trying to fix. Finally be reading #4934 I now understand the purpose of this change, but I'm still puzzled about the default behavior.

What if instead of changing the default we simply allowed the after parameter to cary a value signifying "before any other child of this parent"?

so you would do $animate.enter(child, enter, true) for example to prepend and without the parameter we would still append.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't think this is a good idea since enter has already four params and this would be a fifth one (which comes after the optional callback param).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would not be an extra param, but rather the after argument could optionally take true value in addition the the afterElement

}));

it("should remove the element at the end of leave animation", inject(function($animate, $compile, $rootScope) {
var child = $compile('<div></div>')($rootScope);
element.append(child);
Expand Down