data("notifications") on that object, and we ill get an array of json objects back. This is all the code that we actually need in order to reference that json thats preloaded into the page. So when the page loads, we can go an render immediately. All we're going to have to do, is go into our notification.js.coffee, and let's comment out this code here so that when the page loads, we'll never display notifications. Here let's actually use that preloaded json that we have on the page, and let's just
console.log @notifications.data("notifications"). This is that part that we're already doing in the console, so we just need to use that variable to access the embedded notifications. When our page loads, we should see that same array gets printed out, and it does. This is cool, we've already got that going, and that just means that we now have the array, and as it turns out, that's the exact same data object as we get back from the AJAX request. That means we can just simply call
It might be kind of surprising that we can literally reuse the handleSucess here from rendering after we get the json back, but that is all we really need to do. That means you could actually go through and do this, immediately upon page load, and then you can also use that code for setTimeout just to simulate your browser being slow. You can actually replace this with a setInterval, and you can set it up to check for new notifications every 5 seconds, for example, and then you would get sort of a simulated real time set of notifications. That would be all that you would have to change for that to preload them immediately on the page, and then to check for updates every 5 seconds. It's pretty cool, it allows you to really have it easy to work with notification system, and then the last piece that we have to do is to go back here and replace this hard-coded notification with the actual notifications that we get from generating that json. Let's go ahead and do that now. Let's pull in the json that we hard-coded before, and let's replace this with rendering that exact same template that our application uses for notifications. That's the notifications/index.json.jbuilder file. This is the file that you want to render from inside this other template, and you can actually simply do that by calling
<%= render template: "notification/index", formats: [:json] %> otherwise it won't look for a partial, and here you say "notifications/index", and because this is an html request, you're actually going to need to specify formats as json in order to force it to render the json format. That's really all you're going to have to do template-wise, but remember, this json jbuilder file is rendering the @notifications variable that is set in the notifications controller. This is the unread notifications for the current user, and we're not going to be necessarily inside this controller all the time. This is the application template, so this is for every single page that is rendered, we're going to need this variable to be queried. Now it's going to be pretty quick, and so we're going to be able to do that to preload it if your servers are fast and scaled up to the point where this is going to be making sense to preload this information. So we would actually need to go into the
before_action :set_notifications, if: :user_signed_in? def set_notifications @notifications = Notifications.where(recipient: current_user).unread end
That should be all we need to do in order to set those notifications up dynamically. If we inspect that notifications again, we'll see that it's automatically parsed into a json object because the data notifications is already handled that way, which is pretty nifty, and so our notification.js.coffee really just needs to stay exactly the same and keep this set interval. It might make sense to rename the setup then, and move this out, so you would want to set this up to be run, instead of then, we want to do it there, and then we'll also
getNewNotifications() every 5 seconds instead. That should do it, and of course, getNewNotifications should be set to the instance method there, the @ method, and it probably makes sense to also do this inside the if statement. We'll check to make sure that that object exists before we do any of the work with it. These are really the two set up methods that we're going to have now, and then this will just be called every 5 seconds. That means that we should be able to go back to this page, and then go to our other user and say: Hello, and we should go back to this, and if we wait for about 5 seconds, we should now see that number change to 4, and it does.
Transcript written by Miguel
Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.