Question

I seem to have hit a snag in my app that is causing the browser to go into a very tight loop that never ends.

In the app I have an unload function on a route that increments a value to count and display the number of times a project has been viewed.

   unload: function() {
      var project = projectDocs.findOne(this.params._id);
      // need to increment views value by one
      projectDocs.update(project._id,
        {$inc: {views: 1}}
      );
    }
   });

This works fine, until I try and enable a button on the form based on a role the user is in (using the roles package). The code to enable the button is as follows (just in a div).

 <a href="{{pathFor 'home'}}" class="btn btn-success {{#unless canQuote}} disabled {{/unless}}">Quote</a>

The canQuote helper is

Template.projectPage.helpers({
  canQuote: function(){
    return Roles.userIsInRole(this.userId, ['tradie','admin'])
  },
  isOwner: function(){
    return this.userId == Meteor.userId();
  } 
});

This all works fine UNTIL a user is logged in. The button is disabled and all is good. When a user logs in with the appropriate role, the page simply hangs trying to display, and the viewed count just goes up in what can only be a very tight loop somewhere.

Can someone give me a clue why this is looping?

Ta

Peter.

Was it helpful?

Solution

My guess is the unload function is giving you some grief. When you do a findOne, the var project becomes reactive and any updates to it (like the update you do right below it) are observed. You don't actually need to do the findOne, just update directly since the id is in the params. Try

unload: function() {
      // need to increment views value by one
      projectDocs.update(this.params._id,
        {$inc: {views: 1}}
      );
    }
   });

Edit: actually you are probably better off putting the update into the after hook rather than unload, because the unload might not get called if a user closes the window or something, whereas the after will record the view as long as the action function executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top