iron-meteor / iron-router

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

window.location.search available only in rendered() callback? #1487

Open alochschmied opened 8 years ago

alochschmied commented 8 years ago

We're trying to access window.location.search but after routing, it is available only in rendered() callback and not in created() callback. Is this by design? We're having some difficulties because of this behaviour. The sample code below demonstrates it. After clicking the link here http://localhost:3000/hello?param=val The console output of hello2 template shows:

hello2 created: ?param=val
hello2 rendered: ?newParam=newVal

Shouldn't the newParam be available already in created()?

Router.map(function () {
    this.route('hello', {
        path: '/hello',
        template: 'hello'
    });
    this.route('hello2', {
        path: '/hello2',
        template: 'hello2'
    });
});
if (Meteor.isClient) {
    Template.hello.events({
        'click #link': function (e, t) {
            e.preventDefault();
            Router.go('hello2', null, {query: 'newParam=newVal'});
        }
    });

    Template.hello2.created = function() {
        console.log('hello2 created: ' + window.location.search);
    };
    Template.hello2.rendered = function() {
        console.log('hello2 rendered: ' + window.location.search);
    };
}
<head>
</head>

<body>
</body>

<template name="hello">
    <h1>hello</h1>
    <a href="#" id="link">Problem?</a>
</template>

<template name="hello2">
    <h1>hello2</h1>
</template>
alochschmied commented 8 years ago

We found a workaround by passing this.params as part of the template data. It would still be interesting to know if the window.location.search should work in created.