Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 813
sergi Avatar asked Mar 01 '14 12:03

sergi


2 Answers

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']
});
like image 181
andylash Avatar answered Nov 17 '22 19:11

andylash


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.

like image 40
Dave Avatar answered Nov 17 '22 19:11

Dave