#229 Polling for Changes (revised)
Jan 14, 2013 | 10 minutes | Performance, Ajax
Polling may not be as common today as pushing changes over an open socket, however it is still an effective, simple solution if you do not need instantaneous updates.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Great tutorial! You can even take it a step further and only pull when the user gets down to the comments area. It's relatively easy to get info about where the browser viewport is in proportion to other elements.
Thanks Eric
I like setting the href to 'javascript:' and skipping e.preventDefault()
I thought that too, I used to do e.preventDefault() all the time until I looked up this Stack Overflow article: http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid0
Now I just use javascript:void(0) all the time!
Great explanation of polling, thanks very much. I love how you build on extra functionality in your videos and go basic > intermediate > advanced with the extra features you keep adding on.
Awesome as usual. By the way i was wondering shouldn't this method CommentPoller.poll() be outside the 'if' clause because if the comments is < 0 then it will never poll again
Hi Amunda, if I understand your question correctly, it's not a problem. That if statement is only checking whether the comments container element is on the page. If it is, then we know we're on the right page, and it would call CommentPoller.poll(). if its not, then we dont need to use the JS
Love the example! I also think polling is a viable solution for many applications. Unfortunately, I'm not a big fan of coffee script so JS based episodes like these require a bit more focus on my part...but maybe that means I should start using coffee script?
http://js2coffee.org/
Converter JS -> Coffee and Coffee -> JS maybe that helps
Wow! Great resource! Thanks!
Thanks! I have been looking for something like this.
Hi Ryan, this tutorial is great. Do you think is possible to have a similar tutorial for socket/comet/push interupt service?
I, too, would enjoy something on sockets other than the Faye episode.
Hi Ryan,
Thanks for the great video. I am having an issue where the app/views/comments/index.js.erb isn't being executed for me. I am trying to implement this in my own project where I have a views called "environments" and that is the one I'm trying to poll for changes. The "environments" doesn't have the same relationship as your article and comments example basically on the environments index page I want to poll for changes but it doesn't seem the index.js.erb file is being executed. Any help would be much appreciated.
Thanks,
Jay
Just to provide more information I don't see something similar to yours where when on the article page you see a request coming in where it says:
Rendered comments/index.js.erb
Mine says something like:
Rendered environments/index.html.haml within layouts/application (1.2ms)
So it looks like I had two things missing which might help others. First my controller needed a "format.js" in the respond_to. The other is my request was being sent as html, so I had to switch the request to "$.ajax({ url: $('#group-instances').data('url'), dataType: "script" })" where group-instances was my id.
I do have another issue though where I have other forms on the same page that I am refreshing data and when any of those buttons are pushed I sometime run into a situation where the request is killed. I think I may need to make those form submits into ajax calls as well. Does this sound right? By the way those other forms on the page make actual calls to external services so sometimes it's not instantaneous like it would be if I was using ActiveRecord like in your screencast. Thanks in advance for any help.
I was having troubles with throwing errors with ascii cast example:
turns out that needs to be as in the shownotes:
I get undefined errors with the '->'
Thanks!!!
I am trying to pass this polling off to a web worker because my UI is not responsive for the duration of the polling. I am stuck on calling the ajax request and then returning that to the main thread so that I can append it.
Has anyone else moved something like this to a web worker? If so, how?
I'm using rails 4 and i got this problem after i used this code i got this error in my browser
GET http://172.26.241.33:3000/galleries/2/comments 500 (Internal Server Error) jquery.js?body=1:9667
send jquery.js?body=1:9667
jQuery.extend.ajax jquery.js?body=1:9212
jQuery.(anonymous function) jquery.js?body=1:9358
CommentPoller.request comments.js?body=1:7
and as you see, rails included "/comments" into the url with i didn't expect it. It must be just "/galleries/2"
I suspected it must be the 1st line that makes my project error, but i don't know how or why???
please help me out or do you have another tutorial for rails 4 please let me know.