0

Hi when a user is directed to one page I want to perform a logic check to see if there are any entries in a collection that are missing an attribute and redirect the user if so. Here is sort of my idea of how the code should look but I can't seen to get the subscribe to work in the routes.js file

routes.js

.state('pendingVisits', {
    url: '/pendingVisits',
    templateUrl: ()=> {
      if (Meteor.isCordova) {
        return '/packages/visitry-mobile/client/visits/pending-visits/pending-visits.html';
      } else {
        return '/packages/visitry-browser/client/visits/pending-visits/pending-visits.html';
      }
    },
    controller: 'pendingVisitsCtrl as pendingVisits',
    resolve:{
      feedback: function($location){
        Meteor.subscribe('visits');
        var v = Visits.findOne({feedbackId});
        if(v){
          $location.url('/visits/'+v._id+'/feedback');
        }
      }
    }
});

Basically I'm trying to do something similar to Uber's UI where if there is no feedback for the last event I display the feedback page. Thanks for the help. Let me know if you need any more code or information.

1
  • I read something that said putting this type of logic in the router is an anti-pattern because you would need to wait for the data to load. Can anyone confirm this? It seems like if I put the logic in the pendingVisits controller that view would flash briefly before the user was redirected Commented Mar 29, 2016 at 20:15

1 Answer 1

1

I was able to solve this with Tracker.autorun() Here is the solution I used:

resolve:{
  feedback:function($location){
    const visits = Meteor.subscribe('visits');
    Tracker.autorun(()=>{
      const isReady = visits.ready();
      var visitNeedingFeedback = Visits.findOne({feedbackId:null})
      if(isReady && visitNeedingFeedback){
        console.log("Yes lets go to feedbacks");
        $location.url('/feedback/'+ visitNeedingFeedback._id);
      }else{
        console.log(`Visits data is ${isReady ? 'ready' : 'not ready'}`)
      }
    })
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.