Monday, 15 August 2011

javascript - Using rails how to synchronize variable in controller to the view where the controller is the initiator of the update -



javascript - Using rails how to synchronize variable in controller to the view where the controller is the initiator of the update -

i tried long polling in ajax, client calls server requesting info @ interval. - updating of list in ui initiated client

below code:

server_controller.rb

@@x=0 def results @@x++ render :json => @@x end

index.html.erb

<button id='get_value' class="btn">run</button> <ul id="value_variable_from_controller"><ul> $(document).ready(function() { var getpoll=function getvalue(trigger) { $.ajax({ type: 'get', url: '/server/results/', success: function(data, status) { if (data < 50){ $('#value_variable_from_controller').append("<li>"+data+"</li>"); settimeout(getvalue(true), 1000);}},})} $(document).on("click","#get_value",getpoll);

the question how can oppositely controller has powerfulness initiate/send info , list on html synchronously updating/appending?

this more tricky do, because need additional technology. http , request-respond model can't this. client (browser) can't receive requests server.

you'll end bidirectional communication, because first client inquire server things , later server asks client update. technology doing websockets. rails don't include this, need add together via gems (websockets-gem) or/and event driven, non-blocking server node.js useful well.

there different tutorials out there, give start. here 1 illustration 2 nice little graphics, explain situation.

javascript jquery ruby-on-rails ruby ajax

No comments:

Post a Comment