Tag Cloud
Concourse CI/CD devops prometheus kubernetes monitoring modbus kepware c-programming IoT golang telegram bot python cli urwid elasticsearch aws ecs apache spark scala AWS EMR hadoop webhooks ssl nginx digital-ocean emr apache pig datapipeline found.io elastic-cloud rails try capybara docker capistrano heka bigquery kafka protobuf vim iterm javascript emberjs git scripting dnsmasq bem frontend meteorjs meteorite heroku

EmberJS Computed Properties vs Manual Filter

The problem begins when one of my ‘filter functions’ for the my model (delayed/active tasks lists from all tasks) wasn’t getting reactively updated when the store gets new entries or updates. When I found out why, I thought to myself that this is actually a pretty easy scenario to fall into IMHO.

I realised that I wasn’t actually utilising ember-data but manually creating the filter. In that manner, the activeTasks will only be evaluated on the first run and that subsequent changes to the taskStatus, endDate and startDate were not observed. I basically had to manually manage the lists myself, which is obviously pretty stupid given the powerful capabilities of the reactive-model-updates. The below code was placed in my task controller.

activeTasks: function() {
    return this.get('content').filter(function(m) {
      var today = new Date()
      var endDate = new Date(m.get('endDate'))
      return endDate <= today && m.get('taskStatus') == true
    });
  }.property('@each.taskStatus, @each.endDate')

The Solution

With some searching, I found that this use case should and only be achieved via computed properties. You have to create a computed property in the model file.

// Computed Property, Placed in the model file.
isDelayed: function() {
    var today = new Date();
    var endDate = new Date(this.get('endDate'));
    return endDate > today && this.get('taskStatus') == true
  }.property('endDate', 'taskStatus')

This gives you the computed property - isDelayed to work with in the controller. And this property is also correctly observed by ember-data and will be automatically updated when there are updates to the underlying property in the model.

// Working with the computed property in the controller
delayedTasks: function() {
    return this.filterProperty('isDelayed', true);
  }.property('@each.isDelayed')
comments powered by Disqus