Wrong order for ionic view event


#1

I have made 2 pages, and in each page, I make some modifications before enter and before leaving with :

$scope.$on(’$ionicView.beforeEnter’, function (e, data) {
});
$scope.$on(’$ionicView.beforeLeave’, function (e, data) {
});

I have found that when I move from page 1 to page 2, the ‘before enter’ event from page 2 is catch before the ‘before leave’ event from page 1.
It’s not the good order, you need to leave page 1 before enter page 2, the right order must be :

-> before leave page 1
-> leave page 1
-> before enter page 2
-> enter page 2


#2

Is anyone reading this ? Some one will fix it ?


#3

@ArnaudDev there are other events in the navigation workflow, and the “magic” happens at the delicate interface between two different projects: Ionic and angular-ui-router. It’s not realistic to hope it’ll be refactored unless there’s a really strong and compelling reason, not to mention all the apps that use it and expect it to continue working the way it works now.

I think your best strategy would be to learn how they work (even if you don’t agree with it) and then relocate your code to another stage, one better fit for what you have in mind, within that workflow that’s already there.

Check this, maybe it helps Codepen to help you understand Ionic Views, UI-Router, Cache


#4

Hi, I’m sorry but in my world, you must leave a page before enter a new one.
Unless you find me a good reason to leave it that way I will keep my position.


#5

Hey there. Could you test out the nightly builds? This was an issue with our view events and I believe that it has been fixed :smile:


#6

I update the ionic lib but the issue is still here, the before enter event from the second page is fire before the before leave event from the first page.


#7

One more thing, when you add this in main controller and in sub controller :

$scope.$on(’$ionicView.beforeEnter’, function (e, data) {
});
$scope.$on(’$ionicView.beforeLeave’, function (e, data) {
});

It is launch 2 times, by the controller and the sub controller. But the first is the sub controller and not the main one ?
Why ? I think its a problem here too.


#8

@ArnaudDev see also https://github.com/driftyco/ionic/blob/master/test/unit/angular/directive/navView.unit.js#L1013-L1064

specially this snippet, lines 1052-1063

    expect(order[0]).toEqual('$ionicView.loaded');
    expect(order[1]).toEqual('$ionicView.beforeEnter');
    expect(order[2]).toEqual('$ionicView.enter');
    expect(order[3]).toEqual('$ionicView.afterEnter');
    expect(order[4]).toEqual('$ionicView.loaded');
    expect(order[5]).toEqual('$ionicView.beforeEnter');
    expect(order[6]).toEqual('$ionicView.beforeLeave');
    expect(order[7]).toEqual('$ionicView.enter');
    expect(order[8]).toEqual('$ionicView.leave');
    expect(order[9]).toEqual('$ionicView.afterEnter');
    expect(order[10]).toEqual('$ionicView.afterLeave');
    expect(order[11]).toEqual('$ionicView.unloaded');