Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Meteor Router functions always being run twice

I'm using the Router package from meteorite.

Meteor.Router.add
  "/article/:id": ->
    log "article"
    "article"

Whenever the article page/template gets loaded, the callback function in the Router always gets run twice. I'm trying to use the callback function for incrementing the view count of the articles. So this poses a problem (every inc is done twice).

Is this the intended behavior? Or did I do something wrong?

UPDATE

It's actually not always being run twice. It happens when the page is refreshed or for the first time the browser navigates to the page. Regardless, it still poses a problem.

like image 505
Dave Avatar asked Nov 03 '22 03:11

Dave


1 Answers

UPDATE:

The culprit is the HTML5-History-API package I'm using for IE 8+ routing support. The solution below will serve to help people with the same setup and problem.


Found a solution. But I'm not sure if this is the intended way for handling this.

"/article/:id": (id) ->
   unless this.init
     Session.set "articleId", id
     Meteor.call "incArticleViews", id
   "article"

If there's a better solution, please post it and I'll accept.

like image 181
Dave Avatar answered Nov 08 '22 06:11

Dave