1

I have been playing round with Volt for few days and love the simple bindings it allows. I was trying to show a 'live clock' on the page by using Time.now. I have tried binding Time.now to the various collections from within the controller but it didn't quite work (I had to refresh the page for the new time to show up, beating the purpose). Is there any way to achieve this without the use of Javascript?

1 Answers1

2

Right now we don't have bindings on Time done yet. Its on the todo list, but at the moment you'll have to manually update it. You could do something like this in a lib file, then require it in a controller with `require '{component_name}/lib/{file}'

class Time
  def live
    dep = Volt::Dependency.new
    dep.depend

    Volt::Timers.client_set_timeout(1000) do
      dep.changed!
    end

    self
  end
end

Then in the controller, you can call .live on a Time instance to get back an instance of the Time class that will reactively update.

Let me know if that works. :-)

Ryan
  • 956
  • 7
  • 8
  • Hi Ryan ! I did give it a quick try but didn't seem to work. Here is what i have done: 1.created a new file **app/main/lib/livetime.rb** with the above code. 2.required it in the **main_controller.rb** and stored it in the page collection. `module Main class MainController < Volt::ModelController require 'main/lib/livetime' def index page._time = Time.live end` 3. Modified **index.html** to render: `<:Body>

    The time now is {{ page._time }}

    ` Is something missing or wrong ?
    – Abdul Jaleel Jun 11 '15 at 17:47
  • So if you stored the time, that would keep the time the same. Instead, you can just keep re-generating the current time. (the .live will invalidate it reactively and call the code in the binding again, so you could do:) def current_time Time.now.live end {{ current_time.to_s }} – Ryan Jun 12 '15 at 00:47
  • That didn't solve it :( . I tried calling Time.now.live from the console and got a no method error ('NoMethodError: undefined method `client_set_timeout' for Volt::Timers:Class'). something missing? :) – Abdul Jaleel Jun 12 '15 at 06:17
  • What version of Volt are you on? – Ryan Jun 12 '15 at 17:34
  • That solved it ! I was still running on Volt 0.9.0. Updated my gemfile and its working fine now. Thank you ! – Abdul Jaleel Jun 15 '15 at 18:45