How to properly replace this.stop() with pause() on Iron Router blaze integration
Asked Answered
O

2

8

When I upgrade Iron Router to blaze integration branch, I began receiving this warning:

"You called this.stop() inside a hook or your action function but you should use pause() now instead" 

Chrome console --> iron-router.js:2104 --> client/route_controller.js:193 from package

The code is on client:

Router.before(mustBeSignedIn, {except: ['userSignin', 'userSignup', 'home']});

var mustBeSignedIn = function () {
    if (!Meteor.user()) {
        // render the home template 
        this.redirect('home');
        // stop the rest of the before hooks and the action function 
        this.stop();
        return false;
    }
    return true;
}

I tried replacing this.stop() with: pause(), Router.pause() and this.pause() but still does not work. Also I haven't found pause function on iron-router package.

How do I properly replace this.stop() with pause()?

Thanks

Overdone answered 1/3, 2014 at 12:47 Comment(4)
Can you post the relevant section of your code and the error messages you get when you try replacing?Slalom
@SerkanDurusoy He did.. he changed this.stop() to this.pause() Router.pause() etc. I'm also having this problem, and the redirect is also not working.Armageddon
@Armageddon he had not when I have posted the comment. It seems the answer below covers the solution.Slalom
We'll have to wait as Dave said... thanksOverdone
A
4

I opened an issue on Github about this. Here's the response I got:

Oops I may have not changed the redirect method yet. Just use Router.go as it will work fine now. I will change over this.redirect sometime next week or a PR is welcome. Controllers are now automatically stopped if you change routes in a hook. You can pause the current run by calling the pause method which is passed as a parameter to your hooks and action functions.

Armageddon answered 2/3, 2014 at 20:35 Comment(0)
D
8

From what I can tell the pause function is the first parameter your before hook is getting called with. Not in the docs anywhere, but that's what I gathered from the code and it seems to work.

Here's what I use:

var subscribeAllPlanItems = function (pause) {
    var planId = this.params._id;
    this.subscribe('revenues', planId).wait();
    this.subscribe('expenses', planId).wait();
};

var waitForSubscriptions = function (pause) {
    if (this.ready()) {  //all the subs have come in
      //NProgress.done();
      setPlan(this.params._id);
    } else { //all subscriptions aren't yet ready, keep waiting
      //NProgress.start();
      pause();
    }
};

Router.map(function () {
    this.route('calendar', {
      path: '/calendar/:_id',
      template: 'calendar',
      before: [
        subscribeAllPlanItems,
        waitForSubscriptions
      ],
    });
    //Other routes omitted
});

var requireLogin = function (pause) {
  if (Meteor.loggingIn()) { //still logging in
    pause();
  }
  if (!Meteor.user()) {  //not logged in
    this.render('signIn');
    pause();
  } else { //logged in, life is good
    console.log("requireLogin: logged in");
  }
};

//This enforces login for all pages except the below ones.
Router.before(requireLogin, {
    except: ['landing', 'signUp', 'signIn', 'forgotPassword', 'resetPassword']
});
Definiendum answered 1/3, 2014 at 23:27 Comment(0)
A
4

I opened an issue on Github about this. Here's the response I got:

Oops I may have not changed the redirect method yet. Just use Router.go as it will work fine now. I will change over this.redirect sometime next week or a PR is welcome. Controllers are now automatically stopped if you change routes in a hook. You can pause the current run by calling the pause method which is passed as a parameter to your hooks and action functions.

Armageddon answered 2/3, 2014 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.