Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetTimeout vs. Ember.run.later in an ember app?

Inside my handlebars template:

Today's date: {{currentDate}} Current Time: {{currentTime}} 

Inside my helpers:

Ember.Handlebars.registerBoundHelper 'currentDate', (option) ->   moment().format('LL');  Ember.Handlebars.registerBoundHelper 'currentTime', (option) ->   moment().format('h:mm:ss a'); 

How would I go about updating the currentTime to the view every 1 second?

I have read the Ember recommends Ember.run.later but I can't quite figure out where to put it and how to call it using this helper.

like image 469
Blair Anderson Avatar asked Feb 02 '14 00:02

Blair Anderson


People also ask

What is Ember run?

Module: ember-metal. Runs the passed target and method inside of a RunLoop, ensuring any deferred actions including bindings and views updates are flushed at the end. Normally you should not need to invoke this method yourself.

Why is Ember used?

Ember. js is an open source, free JavaScript client-side framework used for developing web applications. It allows building client side JavaScript applications by providing a complete solution which contains data management and an application flow. The original name of Ember.

Is Ember a backend?

It should also be mentioned that Ember is purely a frontend framework. It has a number of ways of interacting with the backend of your choice, but this backend is not in any way handled by Ember itself.

Is Ember a frontend?

Ember. js is one component of a complete front end stack built and supported by the Ember core team.


1 Answers

You can use Ember.run.later like you would normally use setTimeout

Ember.run.later((function() {   //do something in here that will run in 2 seconds }), 2000); 

I'm not sure of the internals, but I know that integration testing Ember requires that you use run.later (if you don't the test code won't wait for the timeout to finish).

like image 103
Toran Billups Avatar answered Oct 31 '22 03:10

Toran Billups