Skip to content
Browse files

add polling for patella_partial

  • Loading branch information...
1 parent 52b2bf3 commit aacfd0828bcbad460fc2ceb5b017e1b4292ae26c @jdwyah jdwyah committed
Showing with 26 additions and 9 deletions.
  1. +1 −1 README.md
  2. +2 −0 lib/patella/controllers/actions.rb
  3. +23 −8 lib/patella/helpers/patella_partial.rb
View
2 README.md
@@ -23,7 +23,7 @@ Or install it yourself as:
def self.my_slow_method(user_id)
all_notifications_for(User.find(user_id))
end
- patella_reflex :notification_unread_count, :expires_in => 3.minutes, :class_method => true
+ patella_reflex :my_slow_method, :expires_in => 3.minutes, :class_method => true
```
See the tests for more [examples](https://github.com/kbrock/patella/blob/master/test/patella_test.rb)
View
2 lib/patella/controllers/actions.rb
@@ -3,6 +3,8 @@ def index
patella_obj = Patella::Patella.from_key(params[:key])
if patella_obj.loaded?
render :partial => params[:partial], :locals => params.merge(:patella_obj => patella_obj)
+ else
+ render :status => 204, :nothing => true
end
end
end
View
31 lib/patella/helpers/patella_partial.rb
@@ -7,15 +7,30 @@ def patella_partial patella_obj, partial, params = {}
params = params.merge(:partial => partial, :key => key)
"<script>
window.onload = function(){
- new Ajax.Updater('#patella-#{key}', '/patella',
- { method: 'get',
- parameters: #{params.to_json}});
- };
+ (function poll(){
+ setTimeout(function(){
+ $j.ajax({ url: '/patella',
+ dataType: 'html',
+ data: #{params.to_json},
+ success: function(data, textStatus, jqXHR){
+ console.log('statusCode '+jqXHR.statusCode());
+ if(jqXHR.statusCode() == 204){
+ poll();
+ }else{
+ $('patella-#{key}').innerHTML = data;
+ }
+ }
+ });
+ }, 3000);
+ })();
+ }
+
</script>
- <div id='#patella-#{key}'></div>
- <div class='generic-spinner'>
- <img src='/images/indicator.gif', 'alt'='loading...', 'class'='loading-indicator dingbat-icon'/>
- Please wait. Loading data...
+ <div id='patella-#{key}'>
+ <div class='generic-spinner'>
+ <img src='/images/indicator.gif', 'alt'='loading...', 'class'='loading-indicator dingbat-icon'/>
+ Please wait. Loading data...
+ </div>
</div>
".html_safe!
end

0 comments on commit aacfd08

Please sign in to comment.
Something went wrong with that request. Please try again.