tmeasday / meteor-router

MIT License
366 stars 76 forks source link

auto-executing .click event script on page load #31

Closed casualuser closed 11 years ago

casualuser commented 11 years ago

recently started to use Meteor

and used such code:

    Meteor.Router.add({
      '/layout': 'masterLayout',
      '/profile': 'userProfile',
    });

    Template.masterLayout.events({
      'click .profile': Meteor.Router.to('/profile')
    });

and also:

<template name="masterLayout">
      <a class="profile">Profile</a>
</template>

this code causing auto-execute Meteor.Router.to('/profile') redirect while accessing /layout url

checked a little page.js and found that it have by default page([options]) with such a values:

click - bind to click events [true] popstate - bind to popstate [true] dispatch - perform initial dispatch [true]

so after several hours of experiments I've solved issue with this code:

    Template.masterLayout.events({
      'click .profile': function(e) {
          page('/profile')
          e.preventDefault()
        }
    });

it seems it will be useful to add handler for this paje.js options to Meteor.Router.to and also may be set page({click: false}) by default in case it causing weird loop redirect

if there is any other options to handle such a behavior - please let me know about details

any suggestions welcome

tmeasday commented 11 years ago

Hi, you just needed to wrap your original code in a function() { } so it didn't execute immediately. No need to call page() directly.

On the other hand you could just set the href of your <a> to '/profile'.