Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember computed property that observes current time

Is there a way to have a computed property observe the current time (for use in a timestamp)?

For example in my controller I might have a method that looked something like this:

formatLastUpdated:function() {
    return moment(this.get("model.update_ts")).fromNow();
}.property("model.update_ts", "date.now?"),
like image 706
Ben Avatar asked Dec 21 '13 08:12

Ben


1 Answers

The ember-time repository includes a good example of what you are trying to do.

They use a custom view via a handlebars helper. The view sets up a tick function which is called every second via Ember.run.later:

didInsertElement: function() {
  this.tick();
},
tick: function() {
  var nextTick = Ember.run.later(this, function() {
    this.notifyPropertyChange('value');
    this.tick();
  }, 1000);
  this.set('nextTick', nextTick);
},
willDestroyElement: function() {
  var nextTick = this.get('nextTick');
  Ember.run.cancel(nextTick);
}

The notifyPropertyChange call tells the view that the property has changed and so triggers the re-render...

like image 73
vitch Avatar answered Nov 15 '22 05:11

vitch