简体   繁体   中英

How do I access URL parameters in Meteor helper functions?

For some of the pages in my application, I would like to enable a printable mode, wherein some elements of the page are hidden when the URL parameters include ?print=true . Currently I have attempted a workaround that involves sending the URL parameters as a data context thru iron-router.

    this.route('permitsList', {
        path: '/permits/',
        path: '/',
        waitOn: function(){
            return Meteor.subscribe('openPermits');
        },
        data: function(){
            return this.params;
        }
    });

Accompanied by a global handlebars helper which is used to show and hide elements appropriately.

In this case, when the URL contains ?print=true , it's trivial to show and hide the appropriate elements. However, this solution has two major issues. The first, is the fact that some of the pages for which I wish to implement this printable view already have a data context. Overwriting the data context is not viable, and embedding both data contexts into a larger object is rather disgusting. The second issue is that I wish for the printable view to apply not only inside the template itself, but also in the surrounding layout template in which the page is rendered, so that extraneous headers and so on are removed.

Is there any reasonable way to access URL parameters inside of a Spacebars helper without specifying the parameters in the data context through Iron Router?

To sum up as answer: Query parameters can accessed even without meteor

if ( window.location.search.indexOf( '?print=true' ) > -1 ) { ... } 

window.location returns the current URL, window.location.search only the query parameters as string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM