Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Ariff Munshi asked in General

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

Reply

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!

Reply

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.

Reply

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.

Reply

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

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

Reply

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.

Reply

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/

Reply

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

Reply

jQuery.turbolinks gem was deprecated. Any new solution?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.