iron-meteor / iron-router

A client and server side router designed specifically for Meteor.
MIT License
1.98k stars 412 forks source link

Route data computed 3 times #331

Closed softwarerero closed 10 years ago

softwarerero commented 10 years ago

I have a route which needs to increment a counter in the db and put in the data hash. With the code below I see that my helper function is called 3 times and my code provokes an endless loop. Is it me doing wrong or the router?

@route 'contratosNuevo',
    path: '/contratos/nuevo'
    data: () ->
      boleta: RouteHelper.getMandant()

class RouteHelper
  @getMandant: () ->
    console.log "in router"
    boleta = null
    user = Meteor.users.findOne {"_id": Meteor.userId() }
    if user
      mandant = Mandantes.findOne {"_id": user.profile.mandant }
      if mandant
        boleta = mandant.boletaActual + 1
        Mandantes.update(mandant._id, {$set: {boletaActual: boleta}})
    boleta
tmeasday commented 10 years ago

Hey @softwarerero

IR is designed to run the data function reactively. Specifically as your data loads (e.g. user info from the server's user publication, then "Mandant" data however that is published), the data function will re-run each time as it has more data available.

If you want to count the number of times, I would suggest using the load hook, which is guaranteed to run just once per route.