Skip to main content

Re: In-App Navbar Notifications work on page load but not page change

General • Asked by Ariff Munshi

Hey Chris,

Just watched and implemented the code from this screencast and it's working well. However, I realised that it only works on first page load. Subsequent pages do not run the function. Not sure if there's a bug in my code or it's because of turbolinks. Im not using bootstrap but my own implementation of an off-canvas notification (like in Macbooks).

I call all my functions in Application.js. My attempts to call the Notifications function on page change gives: "Uncaught ReferenceError: Notifications is not defined"

I'm not too versed in CoffeeScript and especially not with using classes. How do I call the Notifications function on page:change?

Cheers,
Ariff


I'd recommend checking out jQuery.turbolinks. It's a gem that basically maps the jQuery ready function to the Turbolinks page:change function (and some others) to make all your jQuery code run as expected. It's one of those things that makes using Turbolinks a lot simpler and fixes some of the issues like this.

That should do the trick without having to write any extra code. Let me know if that works for you!


Thanks for the note Chris! Your replies alone are worth the subscription to GoRails.

Anyway never heard of the gem but I'm sure it will come it pretty handy. Will give it a try tomorrow and let you know it it works.


Thanks man! :)

I'll have to do an episode on jQuery.turbolinks soon. Also Turbolinks 5 as well since that should be coming out in the near future.


Thanks it works! Just had to edit some of the previous js code.

p.s. any plans for a yearly subscription plan?


Hey Chris, another question. My app is now using ajax authentication with devise. Upon authentication my js file updates some of the partials and also adds in the notifications ui elements. However I am not able to call the javascript to execute. Not too sure how this class/prototype thing works. Tried calling "new Notifications" but no no avail.

Hope you can help.

Cheers.


I believe you'll have to make your CoffeeScript class global if you want to do that. More info on that: http://justinreid.com/coffeescript-global-classes/


Hey Chris! You're a beauty! After all that head banging it just took one change. Thank you so much!


jQuery.turbolinks gem was deprecated. Any new solution?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.