Skip to content

Instantly share code, notes, and snippets.

@ankurcha
Created June 12, 2012 18:24
Show Gist options
  • Save ankurcha/2919203 to your computer and use it in GitHub Desktop.
Save ankurcha/2919203 to your computer and use it in GitHub Desktop.
Getting pushstate <a> tags to work with Emberjs

I am generating urls as

           {{#each Em.subNav.options}}
                <li {{bindAttr class="isActive:active"}}>
                    {{#if isActive}}
                        <div class="active"><a {{bindAttr href="href"}}>{{label}}</a></div>
                    {{else}}
                        <a {{bindAttr href="href"}}>{{label}}</a>
                    {{/if}}
                </li>
            {{/each}}

And i would like to have these <a> tags to cause pushstate changes rather than cause the page to reload. I have been using ember-routemanager to get the pushstate working and had a click handler setup in the view as:

   didInsertElement: function() {
        logger.info("Setting up click handler for appropriate usage of pushstate");
        $('a[href^="/"]').click(function(evt) {
            evt.preventDefault();
            logger.info("Clicked: %@".fmt($(this).attr('href')));
            window.Dashboard.get('routeManager').set('location', $(this).attr('href').replace(/^\//, ""));
        });
    },

The problem is that this works good for one click and then the click handler seems to go missing ie causes the browser to handle the <a> tag as a regular one cause the whole page to reload.

Any know issues/workaround/suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment