How do i implement a live clock?

108 views Asked by At

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

There are 1 answers

5
Ryan On

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. :-)